From: Michael Neumann Date: 2001-11-19T06:22:58+09:00 Subject: [ruby-talk:25789] Re: Array.new(n, Array.new(m)) weirdness, but "select isn't broken" Chris Reay wrote: > The following occurs ... > > irb(main):001:0> a = Array.new(3, Array.new(3)) > [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]] > irb(main):002:0> a[1][1] = 0 > 0 > irb(main):003:0> a > [[nil, 0, nil], [nil, 0, nil], [nil, 0, nil]] > > .... however I would have expected (and hoped for) ... > [[nil, nil, nil], [nil, 0, nil], [nil, nil, nil]] p a[0].id p a[1].id p a[2].id return all the same id. This is because a = Array.new(3, Array.new(3)) is the same as b = Array.new(3) a = Array.new(3, b) which creates an array with three references to "b" Maybe further versions of Ruby will have a block form like that a = Array.new(3) { Array.new(3) } In this case the block gets evaluated three times and would create three different objects. Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de