# Can you make sense of this Array issue

**URL:** <https://www.ruby-forum.com/t/can-you-make-sense-of-this-array-issue/105183>\
**Category:** Ruby\
**Created:** [May 26, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/can-you-make-sense-of-this-array-issue/105183 "2007-05-26T21:18:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [May 26, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/can-you-make-sense-of-this-array-issue/105183/1 "2007-05-26T21:18:35Z")

</div>

I want to have an array, say 5x5 with all nils unless set to something  
else.  
However, when I set m[0][0] to 0 … all 0 indices are set to zero…  
Hmm…  
never asked for that to be the case.  
There’s some funky stuff going on here.

irb(main):001:0\> n = 5  
=\> 5  
irb(main):002:0\> m = [[nil]\*n]\*n  
=\> [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,  
nil,  
nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]  
irb(main):003:0\> m[0][0] = 0  
=\> 0  
irb(main):004:0\> m  
=\> [[0, nil, nil, nil, nil], [0, nil, nil, nil, nil], [0, nil, nil, nil,  
nil], [0, nil, nil, nil, nil], [0, nil, nil, nil, nil]]

Any ideas?

---

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [May 26, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/can-you-make-sense-of-this-array-issue/105183/2 "2007-05-26T21:27:36Z")

</div>

O I think I did figure this out. It seems the \* operator just duplicates  
the  
pointers in some optimized fashion. If the nxn array is constructed by  
setting each m[i][j] element everything works fine… I should have  
thought  
of that before.

irb(main):001:0\> n = 5  
=\> 5  
irb(main):002:0\> m = []  
=\> []  
irb(main):003:0\> for i in 0…(n-1)  
irb(main):004:1\> m[i] ||= []  
irb(main):005:1\> for j in 0…(n-1)  
irb(main):006:2\> m[i][j] = nil  
irb(main):007:2\> end  
irb(main):008:1\> end  
=\> 0…4  
irb(main):009:0\> m  
=\> [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,  
nil,  
nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]  
irb(main):010:0\> m[0][0] = 0  
=\> 0  
irb(main):011:0\> m  
=\> [[0, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil,  
nil,  
nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]

---

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [May 26, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/can-you-make-sense-of-this-array-issue/105183/3 "2007-05-26T21:58:39Z")

</div>

In message  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), “Roland  
Mai” writes:

> I want to have an array, say 5x5 with all nils unless set to something else.  
> However, when I set m[0][0] to 0 … all 0 indices are set to zero… Hmm…  
> never asked for that to be the case.  
> There’s some funky stuff going on here.

> irb(main):001:0\> n = 5  
> =\> 5  
> irb(main):002:0\> m = [[nil]\*n]\*n

Nothing odd here, you’re making an array containing the SAME n-ary array  
n times.

-s

---

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [May 27, 2007, 2:25am UTC](https://www.ruby-forum.com/t/can-you-make-sense-of-this-array-issue/105183/4 "2007-05-27T02:25:06Z")

</div>

You might be interested in comparing:

Array.new(10, [])  
and  
Array.new(10) {[]}

---

<div class="post-metadata">

**Author:** ![roland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@roland](https://www.ruby-forum.com/u/roland)\
**Post date:** [May 27, 2007, 4:13am UTC](https://www.ruby-forum.com/t/can-you-make-sense-of-this-array-issue/105183/5 "2007-05-27T04:13:15Z")

</div>

Here’s my interpretation so far.

Array.new(10, []) creates an array with [] being duplicated 10 times.  
Basically, each [] is a mirror/pointer to the original [] object.

Array.new(10) {[]} creates the same array but the difference is that  
each  
time [] creates a different object unlike above where the new object was  
merely a pointer.
