On Feb 16, 12:01 pm, "Kyle Schmitt" <kyleaschm... / gmail.com> wrote:
> 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]]

The short answer is "RTFM" - type in
ri Array.new
in your console and you'll see all this described.

The nice longer answer follows.

You basically asked Ruby to do:
  b = Array.new
  a = [b,b,b,b,b,b,b,b,b]
so modifying any particular instance modifies them all.

What you wanted was:
irb(main):001:0> a = Array.new(9){ Array.new }
=> [[], [], [], [], [], [], [], [], []]
irb(main):002:0> a[0] << 1
=> [1]
irb(main):003:0> a
=> [[1], [], [], [], [], [], [], [], []]