Gavin Kistner
9/12/2006 4:37:00 PM
From: ts [mailto:decoux@moulon.inra.fr]
> G> class Foo
> G> def dup
> G> # strange, magical, important things happen here
> G> # which know about all of Foo's instance variables
>
> You can't use #initialize_copy ?
No. In this case, "Foo" is actually REXML::Element. Because these
objects store all their attributes in an Attributes instance, a standard
(shallow) dup leaves them referencing the same attribute set:
irb(main):001:0> require 'rexml/document'
=> true
irb(main):002:0> e1 = REXML::Element.new( 'foo' )
=> <foo/>
irb(main):003:0> e1.attributes[ 'bar' ] = 'whee'
=> "whee"
irb(main):004:0> e2 = e1.dup
=> <foo bar='whee'/>
irb(main):005:0> e2.attributes[ 'la' ] = 'dee da'
=> "dee da"
irb(main):006:0> e1
=> <foo bar='whee' la='dee da'/>
So, I really want to use Element#clone, but it returns an REXML::Element
instance when called from my subclass, and I have no control over its
source code.