# Ruby bug or not?

**URL:** <https://www.ruby-forum.com/t/ruby-bug-or-not/65015>\
**Category:** Ruby\
**Created:** [August 15, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/ruby-bug-or-not/65015 "2006-08-15T18:21:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [August 15, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/ruby-bug-or-not/65015/1 "2006-08-15T18:21:01Z")

</div>

Using 1.8.4 on OSX.

class CopyTest  
attr\_accessor :one  
end

a = CopyTest.new  
a.one = [1,2,3,4]  
b = a.clone  
a.freeze

b.one.each\_with\_index do |item,index|  
b.one.delete\_at(index) if item == 2  
end

# this is supposed to delete the first item in the array that matches

# instead of all matching items

b.one #=\> [1,3,4]

No surprises so far…

a.one #=\> [1,3,4]

Now that is weird.

a #=\> #\<CopyTest:0x35a4ec @one=[1, 3, 4]\>  
b #=\> #\<CopyTest:0x348698 @one=[1, 3, 4]\>

Not the same object, so why does changing one affect the other?

if you do this…

a.one = [1,2,3,4]  
b=a.clone  
b.one = [4,3,2,1]

then things work normally  
a.one #=\> [1,2,3,4]  
b.one #=\> [4,3,2,1]

I suspect there is a bug in the ‘delete\_at’ function or in the way that  
objects are cloned that causes them to refer to the same array in some  
cases.

Thoughts?

a.one = [1,2,3,4] #=\> TypeError: can’t modify frozen object

Strange, I also managed to change a frozen object this way.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [August 15, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/ruby-bug-or-not/65015/2 "2006-08-15T18:27:01Z")

</div>

On Aug 15, 2006, at 11:18 AM, Kevin O. wrote:

> b = a.clone

That’s not a deep copy:

> > class CopyTest  
> > attr\_accessor :one  
> > end  
> > =\> nil
> 
> >

?\> a = CopyTest.new  
=\> #CopyTest:0x337898

> > a.one = [1,2,3,4]  
> > =\> [1, 2, 3, 4]
> 
> > b = a.clone  
> > =\> #\<CopyTest:0x32fc4c @one=[1, 2, 3, 4]\>
> 
> > a.one.object\_id  
> > =\> 1673774
> 
> > b.one.object\_id  
> > =\> 1673774

James Edward G. II

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [August 15, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/ruby-bug-or-not/65015/3 "2006-08-15T18:30:02Z")

</div>

> > > > > “K” == Kevin O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

Write it like this

K\> class CopyTest  
K\> attr\_accessor :one

```
def initialize_copy(obj)
   self.one = obj.one.clone
end

```

K\> end

Guy Decoux

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [August 15, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/ruby-bug-or-not/65015/4 "2006-08-15T18:33:03Z")

</div>

On Wed, Aug 16, 2006 at 01:18:21AM +0900, Kevin O. wrote:

> a.one #=\> [1,3,4]
> 
> Now that is weird.
> 
> a #=\> #\<CopyTest:0x35a4ec @one=[1, 3, 4]\>  
> b #=\> #\<CopyTest:0x348698 @one=[1, 3, 4]\>
> 
> Not the same object, so why does changing one affect the other?

a.instance\_variable\_get(:@one).object\_id  
=\> 177242  
b.instance\_variable\_get(:@one).object\_id  
=\> 177242

The containing instances aren’t the same object but they are composed of  
the  
same array object.

Object#clone copies the instance variables of the object being cloned.

marcel

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [August 15, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/ruby-bug-or-not/65015/5 "2006-08-15T18:51:06Z")

</div>

On Wednesday, August 16, 2006, at 1:29 AM, Marcel Molina Jr. wrote:

> > a.freeze  
> > No surprises so far…  
> > a.instance\_variable\_get(:@one).object\_id  
> > marcel  
> > –  
> > Marcel Molina Jr. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Yeah, one of those things you figure out right after you post it.

It does concern me that after freezing a, I could modify a.one through  
b.

I can see that one may not want to have ‘freeze’ be called recursively  
on all instance variables, but it sure did throw me off in this case.

-Kevin

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)
