Created
October 3, 2013 02:48
-
-
Save davidbella/6803986 to your computer and use it in GitHub Desktop.
Ruby: Hash rearranging with pigeons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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