Skip to content

Instantly share code, notes, and snippets.

@tehpeh
Created May 20, 2011 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tehpeh/982514 to your computer and use it in GitHub Desktop.
Save tehpeh/982514 to your computer and use it in GitHub Desktop.
Collect array to hash
#!/usr/bin/env ruby
input = [["GREY", "1"], ["GREY", "2"], ["GREY", "3"], ["LIME", "1"], ["LIME", "2"], ["LIME", "3"], ["PINK", "1"], ["PINK", "2"], ["PINK", "3"], ["RED", "1"], ["RED", "2"], ["RED", "3"]]
out = {}
input.each do |a|
out[a.first] ||= []
out[a.first] << a.last
end
puts out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment