Last active
August 29, 2015 14:00
-
-
Save kalasoo/11397964 to your computer and use it in GitHub Desktop.
Array of Array appending
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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