# Has any one tried this?

**URL:** <https://www.ruby-forum.com/t/has-any-one-tried-this/124874>\
**Category:** Ruby\
**Created:** [January 3, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/has-any-one-tried-this/124874 "2008-01-03T12:21:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngloom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@ngloom](https://www.ruby-forum.com/u/ngloom)\
**Post date:** [January 3, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/has-any-one-tried-this/124874/1 "2008-01-03T12:21:09Z")

</div>

My ruby version is 1.8.6  
a = Array.new(3,Array.new())  
a[2]\<\<1  
puts a

the result of a is  
[[1], [1], [1]]  
in irb…

I don’t understand why.  
I just wannt insert into a 1 into the third array of a.

---

<div class="post-metadata">

**Author:** ![ngloom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@ngloom](https://www.ruby-forum.com/u/ngloom)\
**Post date:** [January 3, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/has-any-one-tried-this/124874/2 "2008-01-03T12:32:50Z")

</div>

On 3 Jan 2008, at 11:21, Wu Ning wrote:

> My ruby version is 1.8.6  
> a = Array.new(3,Array.new())  
> a[2]\<\<1  
> puts a
> 
> the result of a is  
> [[1], [1], [1]]  
> in irb…
> 
> I don’t understand why.  
> Array.new will insert the second argument you give into the array (3  
> times since that’s what you’ve asked for). However it’s the same array  
> (You can see this quite easily if you do Array.new(3,Array.new).map {|  
> x| x.object\_id}). So a is not an array containing 3 arrays, it’s an  
> array containing the same array 3 times.  
> a = Array.new(3) {[]}  
> should do the trick (since the block is called once for each element  
> of the array)

Fred

---

<div class="post-metadata">

**Author:** ![ngloom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@ngloom](https://www.ruby-forum.com/u/ngloom)\
**Post date:** [January 3, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/has-any-one-tried-this/124874/3 "2008-01-03T12:52:13Z")

</div>

Wu Ning kirjoitti:

> My ruby version is 1.8.6  
> a = Array.new(3,Array.new())  
> a[2]\<\<1  
> puts a

I suppose you wanted to type

irb(main):012:0\> uber = [[], [], 3]  
=\> [[], [], 3]

Csmr

---

<div class="post-metadata">

**Author:** ![ngloom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@ngloom](https://www.ruby-forum.com/u/ngloom)\
**Post date:** [January 3, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/has-any-one-tried-this/124874/4 "2008-01-03T12:35:59Z")

</div>

Wu Ning wrote:

> My ruby version is 1.8.6  
> a = Array.new(3,Array.new())  
> a[2]\<\<1  
> puts a
> 
> the result of a is  
> [[1], [1], [1]]  
> in irb…
> 
> I don’t understand why.  
> I just wannt insert into a 1 into the third array of a.

After a = Array.new(3,Array.new()), a is an array containing three  
references to the same object. There is only one array, but it is  
repeated three times.

Do  
a = []; 3.times { a \<\< [] }  
and you’ll have three different arrays.

best,  
Dan

---

<div class="post-metadata">

**Author:** ![ngloom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@ngloom](https://www.ruby-forum.com/u/ngloom)\
**Post date:** [January 3, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/has-any-one-tried-this/124874/5 "2008-01-03T13:11:34Z")

</div>

Thanks,  
I know what’s going on now,  
🙂

Daniel L. wrote:

> Wu Ning wrote:
> 
> > My ruby version is 1.8.6  
> > a = Array.new(3,Array.new())  
> > a[2]\<\<1  
> > puts a
> > 
> > the result of a is  
> > [[1], [1], [1]]  
> > in irb…
> > 
> > I don’t understand why.  
> > I just wannt insert into a 1 into the third array of a.
> 
> After a = Array.new(3,Array.new()), a is an array containing three  
> references to the same object. There is only one array, but it is  
> repeated three times.
> 
> Do  
> a = []; 3.times { a \<\< [] }  
> and you’ll have three different arrays.
> 
> best,  
> Dan

---

<div class="post-metadata">

**Author:** ![ngloom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@ngloom](https://www.ruby-forum.com/u/ngloom)\
**Post date:** [January 3, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/has-any-one-tried-this/124874/6 "2008-01-03T18:26:28Z")

</div>

On Jan 3, 2008 1:11 PM, Wu Ning [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,  
> I know what’s going on now,  
> 🙂  
> Good 😉 Ruby has however a syntax that will do what you intended in  
> the first place, see below please… and yes err  
> please do not top post unless you have a very good reason to do so in  
> which case I apologize for having mentioned it.
> 
> Daniel L. wrote:
> 
> > Wu Ning wrote:
> > 
> > > My ruby version is 1.8.6  
> > > a = Array.new(3,Array.new())

a = Array.new(3){ [] }

> > > a[2]\<\<1  
> > > puts a

[[],[],[1] ]

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein
