a = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
b = []
row = []
3.times { row << 0 }
3.times { b << row }
p a #=> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
p b #=> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
p a == b #=> true
p (a[1][1] = 1) == (b[1][1] = 1) #=> true
# and yet ...
a[1][1] = 1
b[1][1] = 1
p a #=> [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
p b #=> [[0, 1, 0], [0, 1, 0], [0, 1, 0]]
With this code, I'd expect the modified b to be the same as the modified a, but instead, the second element of each nested array is modified.
What am I missing? Why do a[1][1] = 1
and b[1][1] = 1
produce different results?