# Bug or not a bug? array\*=int

**URL:** https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898
**Category:** ruby-talk
**Created:** [29 October 2008 15:30 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898 "2008-10-29T15:30:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)
#### Post date: [29 October 2008 15:30 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898/1 "2008-10-29T15:30:12Z")

</div>

So I'm wondering, is this a bug, or not a bug, or a type-o in the docs?

According to [http://www.ruby-doc.org/core/classes/Array.html](http://www.ruby-doc.org/core/classes/Array.html)  
array\*int "[R]eturns a new array built by concatenating the int  
copies of self."  
Copies should be brand new independent copies, right?

foo=[[nil,nil,nil]]returns a new array built by concatenating the int  
copies of self.  
foo\*=3  
#Foo is now.  
#[[nil,nil,nil],  
# [nil,nil,nil],  
# [nil,nil,nil]]

foo[1][1]=12  
#Foo is now  
#[[nil,12,nil],  
# [nil,12,nil],  
# [nil,12,nil]]

#But I would have expected it to be  
#[[nil,nil,nil],  
# [nil,12,nil],  
# [nil,nil,nil]]

---

<div class="post-metadata">

### Author: ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)
#### Post date: [29 October 2008 16:12 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898/2 "2008-10-29T16:12:49Z")

</div>

No, copies are shallow copies. Consider, for example

irb(main):002:0\> class A; attr\_accessor :a; end  
=\> nil  
irb(main):003:0\> a = A.new  
=\> #\<A:0xb7c65b44\>  
irb(main):004:0\> a.a = 10  
=\> 10  
irb(main):005:0\> foo = [a]  
=\> [#\<A:0xb7c65b44 @a=10\>]  
irb(main):006:0\> foo \*= 3  
=\> [#\<A:0xb7c65b44 @a=10\>, #\<A:0xb7c65b44 @a=10\>, #\<A:0xb7c65b44 @a=10\>]  
irb(main):007:0\> a.a = 20  
=\> 20  
irb(main):008:0\> foo  
=\> [#\<A:0xb7c65b44 @a=20\>, #\<A:0xb7c65b44 @a=20\>, #\<A:0xb7c65b44 @a=20\>]

martin

> **···**
>
> On Wed, Oct 29, 2008 at 8:30 AM, Kyle Schmitt \<kyleaschmitt@gmail.com\> wrote:
> 
> > So I'm wondering, is this a bug, or not a bug, or a type-o in the docs?
> > 
> > According to [class Array - RDoc Documentation](http://www.ruby-doc.org/core/classes/Array.html)  
> > array\*int "[R]eturns a new array built by concatenating the int  
> > copies of self."  
> > Copies should be brand new independent copies, right?

---

<div class="post-metadata">

### Author: ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)
#### Post date: [29 October 2008 16:26 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898/3 "2008-10-29T16:26:06Z")

</div>

Ahh.  
And upon not being lazy and looking at the C, I see this...  
ary2 = ary\_new(rb\_obj\_class(ary), len);

Which I'm guessing is equivalent to this in ruby (which I already knew  
would return the same array len times)  
ary2=Array.new(ary,len)

Any chance the document maintainer could slip the word "shallow" into  
the description of array\*?

🙂

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [29 October 2008 16:54 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898/4 "2008-10-29T16:54:30Z")

</div>

Dunno. But all (or almost all) copies (e.g. dup, clone, Enumerable#map, Enumerable#select) do shallow copying only. So shallow is the rule and not particularly exceptional - as it is also more efficient and deep copy is more complex (i.e. deciding when to create a new object or just copy the reference). There are quite a few discussions of this topic in the archives...

For deep copies the idiom probably most often used is

copy = Marshal.load(Marshal.dump(obj))

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 29.10.2008 17:26, Kyle Schmitt wrote:
> 
> > Ahh.  
> > And upon not being lazy and looking at the C, I see this...  
> > ary2 = ary\_new(rb\_obj\_class(ary), len);
> > 
> > Which I'm guessing is equivalent to this in ruby (which I already knew  
> > would return the same array len times)  
> > ary2=Array.new(ary,len)
> > 
> > Any chance the document maintainer could slip the word "shallow" into  
> > the description of array\*?

---

<div class="post-metadata">

### Author: ![Stefan\_Rusterholz](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stefan\_Rusterholz](https://rubytalk.org/u/Stefan_Rusterholz)
#### Post date: [30 October 2008 12:22 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898/5 "2008-10-30T12:22:59Z")

</div>

Kyle Schmitt wrote:

> Ahh.  
> And upon not being lazy and looking at the C, I see this...  
> ary2 = ary\_new(rb\_obj\_class(ary), len);
> 
> Which I'm guessing is equivalent to this in ruby (which I already knew  
> would return the same array len times)  
> ary2=Array.new(ary,len)
> 
> Any chance the document maintainer could slip the word "shallow" into  
> the description of array\*?
> 
> 🙂

As mentioned by others, shallow is the default (also to my knowledge in  
most OO languages).  
The moment you wish for deep copy is usually the moment you should  
consider writing a proper class instead of deeply nesting  
arrays/hashes/...

Regards  
Stefan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [29 October 2008 17:37 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898/6 "2008-10-29T17:37:59Z")

</div>

Robert Klemme wrote:

> deep copy is more complex

... and in many cases impossible (e.g. open files/sockets; procs/blocks;  
any object with a singleton class)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)
#### Post date: [30 October 2008 18:54 UTC](https://rubytalk.org/t/bug-or-not-a-bug-array-int/49898/7 "2008-10-30T18:54:43Z")

</div>

Stefan, while I agree that in general you should write a proper class,  
the case I was talking about is a completely valid and normal use of  
multi-dimensional arrays in ruby. I'm guessing that duplicating a row  
in an array is not a rare occurrence either. I was just saying that  
maybe a mention of this should be added to the standard docs, as it's  
a common pitfall. Bits are cheap 🙂 common pitfalls should be noted  
all over the place in the docs.

--Kyle

> **···**
>
> On Thu, Oct 30, 2008 at 7:22 AM, Stefan Rusterholz \<apeiros@gmx.net\> wrote:
> 
> > As mentioned by others, shallow is the default (also to my knowledge in  
> > most OO languages).  
> > The moment you wish for deep copy is usually the moment you should  
> > consider writing a proper class instead of deeply nesting  
> > arrays/hashes/...
> > 
> > Regards  
> > Stefan
