Skip to content

Instantly share code, notes, and snippets.

@kalasoo
Last active August 29, 2015 14:00
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 kalasoo/11397964 to your computer and use it in GitHub Desktop.
Save kalasoo/11397964 to your computer and use it in GitHub Desktop.
Array of Array appending
# Just found an interesting stuff
x = [[]] * 4 # [[], [], [], []]
x[0] << 1 # or x[0] .push 1
# => [[1], [1], [1], [1]]
# Reason
x = [[]] * 4
puts x.each {|a| puts a.object_id }
x = Array.new(4) { [] }
puts x.each {|a| puts a.object_id }
# >> 70261926896280
# >> 70261926896280
# >> 70261926896280
# >> 70261926896280
# >> 70261926887120
# >> 70261926887100
# >> 70261926887060
# >> 70261926887040
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment