# A deepcopy for "array = \[1,2,3\]\*5"?

**URL:** https://rubytalk.org/t/a-deepcopy-for-array-1-2-3-5/18582
**Category:** ruby-talk
**Created:** [23 May 2005 12:20 UTC](https://rubytalk.org/t/a-deepcopy-for-array-1-2-3-5/18582 "2005-05-23T12:20:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ardanwen](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@Ardanwen](https://rubytalk.org/u/Ardanwen)
#### Post date: [23 May 2005 12:20 UTC](https://rubytalk.org/t/a-deepcopy-for-array-1-2-3-5/18582/1 "2005-05-23T12:20:17Z")

</div>

Been looking this up in the myriad of deepcopy strands, but couldn't  
find it in 30 minutes, so I'm posting the question.

The question is simple. What way would you create an array that  
contains 5 copies of an array, without the copies interfering with each  
other?

array = [1,2,3]\*5

generates:  
[[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]]

but unfortunately also makes 'array[1][1] = "a"' change it like this:

[[1,"a",3],[1,"a",3],[1,"a",3],[1,"a",3],[1,"a",3]]

> **···**
>
> ---------  
> Anyway, found a solution during tinkering, although I don't know why.  
> Thought I'd just still post it anyway.
> 
> ar = []  
> 5.times{ar \<\< [1,2,3]}
> 
> works, and 'array[1][1] = "a"' results in the thing I want
> 
> [[1,2,3],[1,"a",3],[1,2,3],[1,2,3],[1,2,3]]

---

<div class="post-metadata">

### Author: ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)
#### Post date: [23 May 2005 12:28 UTC](https://rubytalk.org/t/a-deepcopy-for-array-1-2-3-5/18582/2 "2005-05-23T12:28:08Z")

</div>

array = Array.new(5) { [1,2,3] }

> **···**
>
> On 2005-05-23 21:20:17 +0900, Boris wrote:
> 
> > The question is simple. What way would you create an array that  
> > contains 5 copies of an array, without the copies interfering with each  
> > other?
> 
> --  
> Florian Frank

---

<div class="post-metadata">

### Author: ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)
#### Post date: [23 May 2005 12:29 UTC](https://rubytalk.org/t/a-deepcopy-for-array-1-2-3-5/18582/3 "2005-05-23T12:29:01Z")

</div>

For me [1,2,3]\*5 behaves different.

irb(main):001:0\> [1,2,3]\*5  
=\> [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

For what you want you could use:

irb(main):002:0\> a = Array.new(5) { [1,2,3] }  
=\> [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]  
irb(main):003:0\> a[1][1] = 'a'  
=\> "a"  
irb(main):004:0\> a  
=\> [[1, 2, 3], [1, "a", 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]

$ ruby -v  
ruby 1.8.2 (2005-04-11) [i386-linux]

Which version of ruby are you using?

best regards,

Brian Schröder

> **···**
>
> On 23/05/05, Boris \<bvschmid@gmail.com\> wrote:
> 
> > Been looking this up in the myriad of deepcopy strands, but couldn't  
> > find it in 30 minutes, so I'm posting the question.
> > 
> > The question is simple. What way would you create an array that  
> > contains 5 copies of an array, without the copies interfering with each  
> > other?
> > 
> > array = [1,2,3]\*5
> > 
> > generates:  
> > [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]]
> > 
> > but unfortunately also makes 'array[1][1] = "a"' change it like this:
> > 
> > [[1,"a",3],[1,"a",3],[1,"a",3],[1,"a",3],[1,"a",3]]
> > 
> > ---------  
> > Anyway, found a solution during tinkering, although I don't know why.  
> > Thought I'd just still post it anyway.
> > 
> > ar =   
> > 5.times{ar \<\< [1,2,3]}
> > 
> > works, and 'array[1][1] = "a"' results in the thing I want
> > 
> > [[1,2,3],[1,"a",3],[1,2,3],[1,2,3],[1,2,3]]
> 
> --  
> [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

### Author: ![Premshree\_Pillai1](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Premshree\_Pillai1](https://rubytalk.org/u/Premshree_Pillai1)
#### Post date: [23 May 2005 14:58 UTC](https://rubytalk.org/t/a-deepcopy-for-array-1-2-3-5/18582/4 "2005-05-23T14:58:43Z")

</div>

He prolly meant [[1, 2, 3]]\*5

> **···**
>
> On 5/23/05, Brian Schröder \<ruby.brian@gmail.com\> wrote:
> 
> > For me [1,2,3]\*5 behaves different.
> > 
> > irb(main):001:0\> [1,2,3]\*5  
> > =\> [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
> 
> --  
> Premshree Pillai
