From: Gennady Bystritsky Date: 2008-04-15T10:04:43+09:00 Subject: Re: How to duplicate a object changing the class without attributes memory copy Iñaki Baz Castillo wrote: > El Lunes, 14 de Abril de 2008, Gary Wright escribió: >> If you just do something like: >> >> from_obj.name = header_obj.name >> >> You are simply copying object references, you aren't >> copying all the associated data. > > Hi, that's not true: if an attribute is a String then there will be > memory copy: > > class Header > attr_accessor :name > end > > class From > attr_accessor :name > end > > header = Header.new > header.name = "AAAA" > => "AAAA" > > from = From.new > from.name = header.name > => "AAAA" > > from.name = "BBBB" > => "BBBB" > > header.name > => "AAAA" > > > As you see in the example above if the attribute is a String (or a > Fixnum and so) attribute copy means memory dupplication. > Unfortunatelly in my case the attribute is a String. > > Regards. Not true. To verify it, after you do "from.name = header.name", try this: puts header.name.object_id puts from.name.object_id You will see same object id, meaning that you have a reference, not a new object. To make it a copy, do this: from.name = header.name.dup Gennady.