# Overriding Object#clone vs. Overriding Object#dup for Deep-Cloning

**URL:** <https://www.ruby-forum.com/t/overriding-object-clone-vs-overriding-object-dup-for-deep-cloning/223007>\
**Category:** Ruby\
**Created:** [September 18, 2012, 9:20pm UTC](https://www.ruby-forum.com/t/overriding-object-clone-vs-overriding-object-dup-for-deep-cloning/223007 "2012-09-18T21:20:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 18, 2012, 9:20pm UTC](https://www.ruby-forum.com/t/overriding-object-clone-vs-overriding-object-dup-for-deep-cloning/223007/1 "2012-09-18T21:20:29Z")

</div>

Hi.

Is there a community convention that specifies which of the two methods,  
`Object#clone` and `Object#dup`, should be overridden for the purposes  
of  
deep-cloning?

On Stackoverflow, some say that `clone` should be overridden [1](http://stackoverflow.com/a/8529625/1173), while  
others say that `dup` is the method to be overridden [2](http://stackoverflow.com/a/11752485/1173). So I suspect  
that  
there isn’t such a convention.

---

<div class="post-metadata">

**Author:** ![Yaser\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7bcc69/32.png) [@Yaser\_S](https://www.ruby-forum.com/u/Yaser_S)\
**Post date:** [September 18, 2012, 11:25pm UTC](https://www.ruby-forum.com/t/overriding-object-clone-vs-overriding-object-dup-for-deep-cloning/223007/2 "2012-09-18T23:25:00Z")

</div>

On Tue, Sep 18, 2012 at 9:20 PM, Yaser S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

Those answers are not good because in part they are just plain wrong.  
There is no community convention either way that I am aware of.

My question for you would be: what do you need deep copying for? I  
often find deep copy is a bit of a cargo cult approach: we simply grab  
everything we can get so we are sure it’s ours. I have rarely - if at  
all - felt the need for a deep copy; I usually prefer specific  
functionality to handle state.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Yaser\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7bcc69/32.png) [@Yaser\_S](https://www.ruby-forum.com/u/Yaser_S)\
**Post date:** [September 18, 2012, 11:26pm UTC](https://www.ruby-forum.com/t/overriding-object-clone-vs-overriding-object-dup-for-deep-cloning/223007/3 "2012-09-18T23:26:32Z")

</div>

both methods use internal an method called initialize\_copy

so my opinion is to overwrite this method

---

<div class="post-metadata">

**Author:** ![Yaser\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7bcc69/32.png) [@Yaser\_S](https://www.ruby-forum.com/u/Yaser_S)\
**Post date:** [September 20, 2012, 6:58pm UTC](https://www.ruby-forum.com/t/overriding-object-clone-vs-overriding-object-dup-for-deep-cloning/223007/4 "2012-09-20T18:58:39Z")

</div>

@OP:

There is a dclone() method that is added to the Array class, by the  
library file: “lib/rexml/xpath\_parser.rb”

You could load that file, or steal the method definition for your own  
custom Array subclass.

It is not a good idea to override Base class instance methods, but you  
could if your code will not be released (used only internally.)

Changing base classes, in released packages, could interfere with other  
packages. (Making users and other Ruby authors angry.)

General “rule of thumb”… if you can use a custom subclass, rather than  
modifying a base class, than do so. (A must if your code runs within a  
shared Ruby process where other authors code is also running, such as  
Applications that run Ruby as an embedded scripting host.)
