Josselin
9/24/2007 10:19:00 AM
On 2007-09-24 06:23:59 +0200, Phrogz <phrogz@mac.com> said:
> On Sep 23, 8:08 am, Josselin <josse...@wanadoo.fr> wrote:
>> irb(main):001:0> cat_a = Array.new(7, Array.new)
>> irb(main):002:0> cat_a[0] << 0
>> => [0]
>> irb(main):004:0> cat_a[1] << 1
>> => [0, 1]
>>
>> but it gives me back
>> irb(main):005:0> cat_a
>> => [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
>>
>> I would like to get back
>>
>> [ [0], [1], [], [], [], [], [] ]
>
> irb(main):001:0> cat_a = Array.new(7){ [] }
> => [[], [], [], [], [], [], []]
> irb(main):002:0> cat_a[0] << 0
> => [0]
> irb(main):003:0> cat_a[1] << 1
> => [1]
> irb(main):004:0> cat_a
> => [[0], [1], [], [], [], [], []]
>
>
> What's wrong with it is that you are using the exact same array for
> all 7 spots. You want a new Array to be created for each spot, using
> the block notation above.
thanks a lot.. got it