Skip to content

Instantly share code, notes, and snippets.

@davidbella
Created October 3, 2013 02:48
Show Gist options
  • Save davidbella/6803986 to your computer and use it in GitHub Desktop.
Save davidbella/6803986 to your computer and use it in GitHub Desktop.
Ruby: Hash rearranging with pigeons
pigeon_data = {
:color => {
:purple => ["Theo", "Peter Jr.", "Lucky"],
:grey => ["Theo", "Peter Jr.", "Ms .K"],
:white => ["Queenie", "Andrew", "Ms .K", "Alex"],
:brown => ["Queenie", "Alex"]
},
:gender => {
:male => ["Alex", "Theo", "Peter Jr.", "Andrew", "Lucky"],
:female => ["Queenie", "Ms .K"]
},
:lives => {
"Subway" => ["Theo", "Queenie"],
"Central Park" => ["Alex", "Ms .K", "Lucky"],
"Library" => ["Peter Jr."],
"City Hall" => ["Andrew"]
}
}
pigeon_list = {}
pigeon_data.each_pair do |attribute_name, attribute_hash|
attribute_hash.each_pair do |attribute, name_list|
name_list.each do |name|
pigeon_list[name] ||= {}
pigeon_list[name][attribute_name] ||= []
pigeon_list[name][attribute_name] << attribute.to_s
end
end
end
p pigeon_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment