Skip to content

Instantly share code, notes, and snippets.

@semanticart
Created November 6, 2008 14:24
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 semanticart/22590 to your computer and use it in GitHub Desktop.
Save semanticart/22590 to your computer and use it in GitHub Desktop.
# what is going on with Hash.new([])?
lines = [
[1,2],
[3,4],
[5,6],
[5,6.5],
[7,8]
]
present = Hash.new([])
lines.map do |item|
present[item.first] << item.last
"some important value"
end
# present = {} ?!?!?!?!
present = {}
lines.map do |item|
present[item.first] ||= []
present[item.first] << item.last
"some important value"
end
# present = {5=>[6, 6.5], 1=>[2], 7=>[8], 3=>[4]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment