When trying to append to an array that lives in an array, it appends
to all until the individual array is used with =
Here's exactly what I did
irb(main):007:0> a=Array.new(9,Array.new())
I expect and I get [[], [], [], [], [], [], [], [], []]
irb(main):008:0> a[0]<<1
I expect [[1], [], [], [], [], [], [], [], []]
but I get [[1], [1], [1], [1], [1], [1], [1], [1], [1]]
irb(main):009:0> a[0]<<1
I expect [[1,1], [], [], [], [], [], [], [], []]
but I get [[1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1,
1], [1, 1]]
irb(main):010:0> a[0]=[1,2,3]
I expect [[1,2,3], [], [], [], [], [], [], [], []]
but I get [[1,2,3], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1],
[1, 1], [1, 1]]
So once more
irb(main):011:0> a[0]<<1
originally I would have expected
I expect [[1,2,3,1], [], [], [], [], [], [], [], []]
now because of this wacky behavior I expect
[[1, 2, 3, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1],
[1, 1,1], [1, 1, 1], [1, 1, 1]]
but what actually happens? This
[[1, 2, 3, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1]]
What on earth is happening?
--Kyle
PS: I do have a work around so my code works as I expected it, using
+=[i] instead of <<i, but I really want to know _why_ here.