On 10/14/09, Robert Klemme <shortcutter / googlemail.com> 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 looking at these 2 sentences:

     In general, +clone+ and +dup+ may have different
     semantics in descendent classes. While +clone+ is used to duplicate
     an object, including its internal state, +dup+ typically uses the
     class of the descendent object to create the new instance.

Frankly, I've never been real sure what this is supposed to mean, so
my reading may well be wrong. In fact, it probably is.

initialize_copy apparently is used by both dup and clone. You're
right, that should be defined (overridden?) instead of dup/clone
themselves. I rarely remember that.

> 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

Ah, yes. But only frozen state, not tainted.