On 10/15/2009 08:07 PM, Caleb Clausen wrote: > 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. Mee, too. :-) Just for the reference irb(main):019:0> class X irb(main):020:1> def initialize_copy(*a) p [self,a] end irb(main):021:1> end => nil irb(main):022:0> x=X.new => #<X:0x8670108> irb(main):023:0> x.dup [#<X:0x865d9cc>, [#<X:0x8670108>]] => #<X:0x865d9cc> irb(main):024:0> x.clone [#<X:0x864ddb0>, [#<X:0x8670108>]] => #<X:0x864ddb0> And it's noteworthy that frozen state is only established _after_ initialize_copy has returned: irb(main):025:0> class X irb(main):026:1> def initialize_copy(old) irb(main):027:2> @x=1 irb(main):028:2> end irb(main):029:1> end => nil irb(main):030:0> X.new.freeze.clone => #<X:0x86451b0 @x=1> >> 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. Right you are. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/