I remember when using C++ for the first time and running into the shallow/deep
copy problem. “I copied my object, freed something in obj_b, but it’s also gone
in obj_a, what gives!?!”
I’m a Ruby Newbie now and wonder how this issue is addressed in Ruby. I’ve seen
references to a clone() method, is this related?
Can anyone enlighten me on this topic with respect to Ruby?
I remember when using C++ for the first time and running into the
shallow/deep
copy problem. “I copied my object, freed something in obj_b, but it’s also
gone
in obj_a, what gives!?!”
I’m a Ruby Newbie now and wonder how this issue is addressed in Ruby. I’ve
seen
references to a clone() method, is this related?
Someone here knows more about this than I and I’ve never really worked with
deep copy, anyway …
clone is a flat copy.
In Ruby, any value deep or flat, integer or complex object, is stored in an
object as a reference.
All references are simply copied using clone, not the content of the
references. Not what you want.
The marshal interface serializes an object to string, including references.
A deep copy can be made using marshal, search google groups for marshal and
copy. There are limitations to using marshal - there could be circular
references and you could be referencing too much data.
Are you really sure you need deep copy?
In all the time I’ve developed software I haven’t needed deep copy ever,
although it is some of those things you initially wonder about.
I’d claim the need for deep copy indicates a design problem except for a few
cases:
Object creation by prototyping - for example copying drawings in a
drawing program
Remote object creation in distributed computing (see marshal, drb).
Actually there was a pretty ugly deep copy bug related to 1) in an early
Ration Rose version. Delete one of the carefully designed boxes, and all the
copies disappeared like magic.
I remember when using C++ for the first time and running into the
shallow/deep copy problem. “I copied my object, freed something in
obj_b, but it’s also gone in obj_a, what gives!?!”
In addition to the other answers, there are two methods to copy an
object in Ruby. Object.dup and Object.clone.
I’m not sure why – they don’t do anything different in the standard
classes. The Pickaxe book says they might do something different in
derived classes, but stops short of suggesting a practice (e.g. clone
does deep copy and dup does shallow copy).
clone is a flat copy.
In Ruby, any value deep or flat, integer or complex object, is stored in an
object as a reference.
It doesn’t make sense to say a value is flat or deep.
All references are simply copied using clone, not the content of the
references. Not what you want.
When you do #clone, it really makes a duplicate of the object. The
difference between a ‘shallow copy’ versus a ‘deep copy’ is the extent
of the duplication. A shallow copy will only duplicate the #clone’d
object. A deep copy duplicates the #clone’d object and recursively #clone all objects that it referes to.
Are you really sure you need deep copy?
There are some valid usages, a few, not many.
The easiest way to do deep copy is by using module Marshall:
In addition to the other answers, there are two methods to copy an
object in Ruby. Object.dup and Object.clone.
I’m not sure why – they don’t do anything different in the standard
classes. The Pickaxe book says they might do something different in
derived classes, but stops short of suggesting a practice (e.g. clone
does deep copy and dup does shallow copy).
Tuesday, December 17, 2002, 11:25:00 AM, you wrote:
I’m not sure why – they don’t do anything different in the standard
classes. The Pickaxe book says they might do something different in
derived classes, but stops short of suggesting a practice (e.g. clone
does deep copy and dup does shallow copy).
anyone can tell why ruby don’t have deep copy routine in standard distribution?
Tuesday, December 17, 2002, 11:25:00 AM, you wrote:
I’m not sure why – they don’t do anything different in the standard
classes. The Pickaxe book says they might do something different in
derived classes, but stops short of suggesting a practice (e.g. clone
does deep copy and dup does shallow copy).
anyone can tell why ruby don’t have deep copy routine in standard distribution?
flori@lambda flori$ irb --prompt xmp
VERSION
==>“1.7.3”
o = [“foo”]
==>[“foo”]
oc = o.deep_clone
==>[“foo”]
o[0].equal? oc[0]
==>false
I just did a CVS get this morning…