Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Created October 3, 2013 01:08
Show Gist options
  • Select an option

  • Save ivanbrennan/6803045 to your computer and use it in GitHub Desktop.

Select an option

Save ivanbrennan/6803045 to your computer and use it in GitHub Desktop.
NYC Pigeon Organizer
########################
# NYC PIGEON ORGANIZER #
########################
# Start with the following collected data on NYC pigs.
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"]
}
}
pigs = {}
pigeon_data.each do |attr, vals|
vals.each do |val, names|
names.each do |name|
pigs[name] ||= {}
if attr == :color
(pigs[name][attr] && pigs[name][attr] << val.to_s) ||
pigs[name][attr] = [val.to_s]
else
pigs[name][attr] = val.to_s
end
end
end
end
# Iterate over the hash above collecting each pigeon by name and insert it
# as the key of a new hash where each name holds the attrs for that bird.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment