# Probleme with 2-dimensionnal arrays

**URL:** <https://www.ruby-forum.com/t/probleme-with-2-dimensionnal-arrays/58213>\
**Category:** Ruby\
**Created:** [April 19, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/probleme-with-2-dimensionnal-arrays/58213 "2006-04-19T18:03:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![z720](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@z720](https://www.ruby-forum.com/u/z720)\
**Post date:** [April 19, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/probleme-with-2-dimensionnal-arrays/58213/1 "2006-04-19T18:03:08Z")

</div>

I can’t understand why this piece of code doesn’t return the array  
expected ?

my\_array = Array.new(3, Array(3))

3.times do |i|  
3.times do [j|  
my\_array[i][j] = i + j  
end  
end

it should return :  
[[0, 1, 2] [1, 2, 3] [2, 3, 4]]

but it returns :  
[[2, 3, 4] [2, 3, 4] [2, 3, 4]]

Is my code wrong ?  
If so how could I build the expected array ?

Thank you

---

<div class="post-metadata">

**Author:** ![z720](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@z720](https://www.ruby-forum.com/u/z720)\
**Post date:** [April 19, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/probleme-with-2-dimensionnal-arrays/58213/2 "2006-04-19T18:11:28Z")

</div>

It has to do with how you construct your array. Your outer array  
contains 3 references to the same inner array.

You need to force each inner array to be a separate object, which can  
be done like this:

my\_array = Array.new(3) { Array.new(3) }

---

<div class="post-metadata">

**Author:** ![z720](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@z720](https://www.ruby-forum.com/u/z720)\
**Post date:** [April 19, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/probleme-with-2-dimensionnal-arrays/58213/3 "2006-04-19T18:14:19Z")

</div>

Matthew M. wrote:

> It has to do with how you construct your array. Your outer array  
> contains 3 references to the same inner array.
> 
> You need to force each inner array to be a separate object, which can  
> be done like this:
> 
> my\_array = Array.new(3) { Array.new(3) }

Perfect thank you very much…

---

<div class="post-metadata">

**Author:** ![z720](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@z720](https://www.ruby-forum.com/u/z720)\
**Post date:** [April 19, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/probleme-with-2-dimensionnal-arrays/58213/4 "2006-04-19T19:57:30Z")

</div>

On Apr 19, 2006, at 11:03 AM, Erard Sebastien wrote:

> I can’t understand why this piece of code doesn’t return the array  
> expected ?
> 
> my\_array = Array.new(3, Array(3))

my\_array = Array.new(3) { Array.new }

> [[2, 3, 4] [2, 3, 4] [2, 3, 4]]
> 
> Is my code wrong ?

The two argument form of the constructor uses the same object in each  
slot. Thus, if you change one, all are modified. The block form I  
used above will be called to generate each object, getting you the  
desired results.

Hope that helps.

James Edward G. II
