# Nested array filling bad

**URL:** <https://www.ruby-forum.com/t/nested-array-filling-bad/115307>\
**Category:** Ruby\
**Created:** [September 23, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/nested-array-filling-bad/115307 "2007-09-23T16:10:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 23, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/nested-array-filling-bad/115307/1 "2007-09-23T16:10:34Z")

</div>

I try to fill a nested array this way

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], [], [], [], [], [] ]

and adding cat\_a[0] \<\< 9  
would give

[[0, 9], [1], [], [], [], [], [] ]

what’s wrong with my array def ?

thanks

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 23, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/nested-array-filling-bad/115307/2 "2007-09-23T16:23:21Z")

</div>

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], [], [], [], [], []]

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 24, 2007, 6:26am UTC](https://www.ruby-forum.com/t/nested-array-filling-bad/115307/3 "2007-09-24T06:26:42Z")

</div>

On Sep 23, 8:08 am, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 24, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/nested-array-filling-bad/115307/4 "2007-09-24T12:22:41Z")

</div>

On 2007-09-23 16:23:21 +0200, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> Returns a new array. In the first form, the new array is empty. In  
> []
> 
> [[0], [1], [], [], [], [], []]  
> thanks a lot… got it

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 24, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/nested-array-filling-bad/115307/5 "2007-09-24T12:20:40Z")

</div>

On 2007-09-24 06:23:59 +0200, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > 
> 
> 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
