From: MikkelFJ Date: 2002-12-17T05:03:37+09:00 Subject: Re: Deep copy in Rub? wrote in message news:H00019f009c894c4@MHS... > > 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: 1) Object creation by prototyping - for example copying drawings in a drawing program 2) 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. Mikkel