From: Rajinder Yadav Date: 2009-10-15T10:39:54+09:00 Subject: Re: deep cloning, how? Robert Klemme wrote: > On 14.10.2009 19:03, Caleb Clausen wrote: > >> The documentation of Object#dup seems to suggest that subclasses >> should not override dup, preferring to override clone instead. > > Where do you take that from? In the docs referenced below I cannot find > anything like that. The only indication I can see is that #dup uses > #initialize_copy and we should probably override that instead of #dup > itself. > >> I'm not >> sure why this should be or why overriding dup would be bad. But >> anyway, I would suggest this: >> >> def deep_clone >> copy=clone >> copy.name=@name.clone >> copy >> end >> >> just so you can keep the existing semantics of clone as a shallow copy. >> >> I'm really not sure why there are 2 methods to create shallow copies >> in ruby and what all the differences are supposed to be. Other than >> not overriding dup(?), the only other difference between them that I >> can discover is that clone copies the metaclass of the object, whereas >> dup reverts the copy's metaclass to being just its class. I've been >> wondering about the difference between the 2 recently; I hope someone >> out there can provide some enlightenment on why there are 2 and what >> the differences are. > > There are more differences namely in the area of frozen and tainted state. > > http://www.ruby-doc.org/core/classes/Object.html#M000351 > http://www.ruby-doc.org/core/classes/Object.html#M000352 > Thanks for the links and solutions Robert. This one got some good replies. > Kind regards > > robert > -- Kind Regards, Rajinder Yadav http://DevMentor.org Do Good ~ Share Freely