Marcel Molina Jr.
8/15/2006 4:29:00 PM
On Wed, Aug 16, 2006 at 01:18:21AM +0900, Kevin Olbrich wrote:
> 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?
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
--
Marcel Molina Jr. <marcel@vernix.org>