# Newbiest ? ever

**URL:** <https://rubytalk.org/t/newbiest-ever/10068>\
**Category:** ruby-talk\
**Created:** [9 February 2004 17:56 UTC](https://rubytalk.org/t/newbiest-ever/10068 "2004-02-09T17:56:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orion\_Hunter](https://avatars.discourse-cdn.com/v4/letter/o/f1d935/32.png) [@Orion\_Hunter](https://rubytalk.org/u/Orion_Hunter)\
**Post date:** [9 February 2004 17:56 UTC](https://rubytalk.org/t/newbiest-ever/10068/1 "2004-02-09T17:56:28Z")

</div>

(ruby 1.8.0 Linux)

I don’t understand why I get the following behavriour:

irb(main):001:0\> matrix = Array.new( 3, Array.new( 4, 0 ) )  
=\> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]  
irb(main):002:0\> matrix[0][0] = 1  
=\> 1  
irb(main):003:0\> matrix  
=\> [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]  
irb(main):004:0\>

I would have expected the assignment of matrix[0][0] to produce a matrix:  
[[1,0,0,0],[0,0,0,0][0,0,0,0]]

What am I not getting here?

> **···**
>
> * * *
> 
> Plan your next US getaway to one of the super destinations here.  
> [http://special.msn.com/local/hotdestinations.armx](http://special.msn.com/local/hotdestinations.armx)

---

<div class="post-metadata">

**Author:** ![Kent\_S1](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kent\_S1](https://rubytalk.org/u/Kent_S1)\
**Post date:** [9 February 2004 18:04 UTC](https://rubytalk.org/t/newbiest-ever/10068/2 "2004-02-09T18:04:57Z")

</div>

Because, matrix = Array.new( 3, Array.new( 4, 0 ) )  
creates an array with three elements all poining to the same array  
object. What you need is something like that:

matrix = Array.new( 3 ) { Array.new( 4, 0 ) }

/kent

Orion Hunter wrote:

> **···**
>
> > (ruby 1.8.0 Linux)
> > 
> > I don’t understand why I get the following behavriour:
> > 
> > irb(main):001:0\> matrix = Array.new( 3, Array.new( 4, 0 ) )  
> > =\> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]  
> > irb(main):002:0\> matrix[0][0] = 1  
> > =\> 1  
> > irb(main):003:0\> matrix  
> > =\> [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]  
> > irb(main):004:0\>
> > 
> > I would have expected the assignment of matrix[0][0] to produce a matrix:  
> > [[1,0,0,0],[0,0,0,0][0,0,0,0]]
> > 
> > What am I not getting here?
> > 
> > * * *
> > 
> > Plan your next US getaway to one of the super destinations here.  
> > [http://special.msn.com/local/hotdestinations.armx](http://special.msn.com/local/hotdestinations.armx)

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard4](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@Laurent\_Julliard4](https://rubytalk.org/u/Laurent_Julliard4)\
**Post date:** [9 February 2004 20:14 UTC](https://rubytalk.org/t/newbiest-ever/10068/3 "2004-02-09T20:14:58Z")

</div>

Array.new(num,object) just creates num refernces to object:  
irb(main):001:0\> a=Array.new(3,‘yo’)  
=\> [“yo”, “yo”, “yo”]  
irb(main):002:0\> a[0].id  
=\> 20896272  
irb(main):003:0\> a[1].id  
=\> 20896272

what you want is different object with the same value, you can do this  
with:  
irb(main):004:0\> a=Array.new(3) {‘yo’}  
=\> [“yo”, “yo”, “yo”]  
irb(main):005:0\> a[0].id  
=\> 20863668  
irb(main):006:0\> a[1].id  
=\> 20863656

so you should write:

matrix= Array.new(3) do  
Array.new(4,0)  
end

or just do:  
require ‘matrix’  
🙂

> **···**
>
> il Tue, 10 Feb 2004 02:56:28 +0900, “Orion Hunter” [orion2480@hotmail.com](mailto:orion2480@hotmail.com) ha scritto::
> 
> > (ruby 1.8.0 Linux)
> > 
> > I don’t understand why I get the following behavriour:
> > 
> > irb(main):001:0\> matrix = Array.new( 3, Array.new( 4, 0 ) )  
> > =\> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]  
> > irb(main):002:0\> matrix[0][0] = 1  
> > =\> 1  
> > irb(main):003:0\> matrix  
> > =\> [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]  
> > irb(main):004:0\>
> > 
> > I would have expected the assignment of matrix[0][0] to produce a matrix:  
> > [[1,0,0,0],[0,0,0,0][0,0,0,0]]
> > 
> > What am I not getting here?
