ruby - Excluding one list of items from another -
i have yml file looks this:
level 1: seattle: name: "rick" id: "95155" time: "2:00 pm" car: "hyundai" denver: name: "arnold" id: "82594" time: "2:00 pm" car: "mitsubishi" level 2: san antonio: name: "james" id: "96231" time: "2:00 pm" car: "honda" minneapolis: name: "ron" id: "73122" time: "2:00 pm" car: "dodge
i need read id
values array processing, remove them array. ways go this?
you can read id values array processing in below way :
require 'yaml' yml = <<-_end_ --- level1: seattle: name: "rick" id: "95155" time: "2:00 pm" car: "hyundai" denver: name: "arnold" id: "82594" time: "2:00 pm" car: "mitsubishi" level 2: san antonio: name: "james" id: "96231" time: "2:00 pm" car: "honda" minneapolis: name: "ron" id: "73122" time: "2:00 pm" car: "dodge" _end_ hsh = yaml.load(yml) # => {"level1"=> # {"seattle"=> # {"name"=>"rick", "id"=>"95155", "time"=>"2:00 pm", "car"=>"hyundai"}, # "denver"=> # {"name"=>"arnold", # "id"=>"82594", # "time"=>"2:00 pm", # "car"=>"mitsubishi"}}, # "level 2"=> # {"san antonio"=> # {"name"=>"james", "id"=>"96231", "time"=>"2:00 pm", "car"=>"honda"}, # "minneapolis"=> # {"name"=>"ron", "id"=>"73122", "time"=>"2:00 pm", "car"=>"dodge"}}} def hash_value(hsh) keys = hsh.keys keys.each_with_object([]){|e,ar| hsh[e].is_a?(hash) ? ar << hash_value(hsh[e]).flatten.uniq : ar << hsh["id"]}.flatten end hash_value(hsh) # => ["95155", "82594", "96231", "73122"]
Comments
Post a Comment