Skip to content

Instantly share code, notes, and snippets.

@octosteve
Created June 18, 2012 00:20
Show Gist options
  • Save octosteve/2946150 to your computer and use it in GitHub Desktop.
Save octosteve/2946150 to your computer and use it in GitHub Desktop.
My version of today's excercise (Hashes)
songs = [
"The Magnetic Fields - 69 Love Songs - Parades Go By",
"The Magnetic Fields - Get Lost - Smoke and Mirrors",
"The Magnetic Fields - Get Lost - You, Me, and the Moon",
"The Magnetic Fields - 69 Love Songs - The Book of Love",
"Neutral Milk Hotel - In An Aeroplane Over the Sea - Holland 1945",
"Neutral Milk Hotel - In An Aeroplane Over the Sea - The King of Carrot Flowers"
]
music = Hash.new {|k,v| k[v] = (Hash.new {|k,v| k[v] = []})}
songs.each do |song|
parts = song.split(" - ")
artist = parts[0]
album = parts[1]
song = parts[2]
music[artist][album] << song
end
puts music
# => {"The Magnetic Fields"=>{"69 Love Songs"=>["Parades Go By", "The Book of Love"], "Get Lost"=>["Smoke and Mirrors", "You, Me, and the Moon"]}, "Neutral Milk Hotel"=>{"In An Aeroplane Over the Sea"=>["Holland 1945", "The King of Carrot Flowers"]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment