From: Josselin Date: 2007-09-24T19:20:04+09:00 Subject: Re: Nested array filling bad.. On 2007-09-23 16:23:21 +0200, 7stud -- said: > Josselin wrote: >> >> what's wrong with my array def ? >> > > --------------------------------------- Array::new > Array.new(size=0, obj=nil) > Array.new(array) > Array.new(size) {|index| block } > ------------------------------------------------------- > Returns a new array. In the first form, the new array is empty. In > the second it is created with _size_ copies of _obj_ (that is, > _size_ references to the same _obj_). > > So, you are essentially doing this: > > a = Array.new > b = a > p a, b > > --output:-- > [] > [] > > > a << 1 > p a, b > > --output:-- > > [1] > [1] > > > Try this instead: > > arr = Array.new(7) {Array.new} > arr[0] << 0 > arr[1] << 1 > > p arr > > --output:-- > [[0], [1], [], [], [], [], []] thanks a lot.. got it