# Strange behaviour with array of arrays

**URL:** https://rubytalk.org/t/strange-behaviour-with-array-of-arrays/34169
**Category:** ruby-talk
**Created:** [2 January 2007 21:14 UTC](https://rubytalk.org/t/strange-behaviour-with-array-of-arrays/34169 "2007-01-02T21:14:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Dan\_Stevens\_IAmAI](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@Dan\_Stevens\_IAmAI](https://rubytalk.org/u/Dan_Stevens_IAmAI)
#### Post date: [2 January 2007 21:14 UTC](https://rubytalk.org/t/strange-behaviour-with-array-of-arrays/34169/1 "2007-01-02T21:14:54Z")

</div>

Please observe the following code:

# Create an array of arrays by explicitly stating values  
test1 = [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]  
=\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]

# Create an array of arrays using new method  
test2 = Array.new(3, Array.new(3))  
=\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]

#Both arrays appear to be equal  
test1 == test2 =\> true

# Modifying one of values behaves as expected  
test1[1][1] = true =\> true  
test1 =\> [[nil, nil, nil], [nil, true, nil], [nil, nil, nil]]

# Modifying one of values behaves strangely  
test2[1][1] = true =\> true  
test2 =\> [[nil, true, nil], [nil, true, nil], [nil, true, nil]]

Can anyone explain why this is happening as I don't understand why the  
two array should behave differently? This is assuming this isn't a  
bug.

---

<div class="post-metadata">

### Author: ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)
#### Post date: [2 January 2007 21:21 UTC](https://rubytalk.org/t/strange-behaviour-with-array-of-arrays/34169/2 "2007-01-02T21:21:21Z")

</div>

> Please observe the following code:
> 
> # Create an array of arrays by explicitly stating values  
> test1 = [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]  
> =\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
> 
> # Create an array of arrays using new method  
> test2 = Array.new(3, Array.new(3))  
> =\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
> 
> #Both arrays appear to be equal  
> test1 == test2 =\> true
> 
> # Modifying one of values behaves as expected  
> test1[1][1] = true =\> true  
> test1 =\> [[nil, nil, nil], [nil, true, nil], [nil, nil, nil]]
> 
> # Modifying one of values behaves strangely  
> test2[1][1] = true =\> true  
> test2 =\> [[nil, true, nil], [nil, true, nil], [nil, true, nil]]
> 
> Can anyone explain why this is happening as I don't understand why the  
> two array should behave differently? This is assuming this isn't a  
> bug.

Let me see if I can get Ruby to explain this for you:

\>\> a = [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]  
=\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]  
\>\> a.map { |e| e.object\_id }  
=\> [3797062, 3797052, 3797042]  
\>\> a = Array.new(3, Array.new(3))  
=\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]  
\>\> a.map { |e| e.object\_id }  
=\> [3779002, 3779002, 3779002]  
\>\> a = Array.new(3) { Array.new(3) }  
=\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]  
\>\> a.map { |e| e.object\_id }  
=\> [3760242, 3760232, 3760222]

As you can see, the new(n, obj) constructor uses the same object over and over again. You want the block form (shown last) instead.

James Edward Gray II

> **···**
>
> On Jan 2, 2007, at 3:14 PM, Dan Stevens (IAmAI) wrote:

---

<div class="post-metadata">

### Author: ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)
#### Post date: [2 January 2007 21:22 UTC](https://rubytalk.org/t/strange-behaviour-with-array-of-arrays/34169/3 "2007-01-02T21:22:54Z")

</div>

The syntax you want is this ...

test2 = Array.new(3) {Array.new(3)}

This will create a new array of three elements and then create a a new  
array object for each element of test2

test2 = Array.new(3, Array.new(3))

This will create a new array of three elements but use the same object  
for element of test2

Blessings,  
TwP

> **···**
>
> On 1/2/07, Dan Stevens (IAmAI) \<dan.stevens.iamai@gmail.com\> wrote:
> 
> > Please observe the following code:
> > 
> > # Create an array of arrays by explicitly stating values  
> > test1 = [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]  
> > =\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
> > 
> > # Create an array of arrays using new method  
> > test2 = Array.new(3, Array.new(3))  
> > =\> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
> > 
> > #Both arrays appear to be equal  
> > test1 == test2 =\> true
> > 
> > # Modifying one of values behaves as expected  
> > test1[1][1] = true =\> true  
> > test1 =\> [[nil, nil, nil], [nil, true, nil], [nil, nil, nil]]
> > 
> > # Modifying one of values behaves strangely  
> > test2[1][1] = true =\> true  
> > test2 =\> [[nil, true, nil], [nil, true, nil], [nil, true, nil]]
> > 
> > Can anyone explain why this is happening as I don't understand why the  
> > two array should behave differently? This is assuming this isn't a  
> > bug.
