[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Duplication from a Subclass, Object#become

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.

2 Answers

Gavin Kistner

9/13/2006 4:25:00 AM

0

Gavin Kistner wrote:
> 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.

FWIW, I found that I was able to do (typing from memory):

class Foo < REXML::Element
def clone
klone = self.class.superclass.method( :new ).unbind.bind(
self.class ).call( self )
# my stuff here
klone
end
end

....but I think I'll probably end up re-writing my class to
wrap/delegate to Element, instead of directly inheriting from it. Pity,
from an rdoc standpoint.

Trans

9/13/2006 1:38:00 PM

0


Phrogz wrote:
> Gavin Kistner wrote:
> > 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.
>
> FWIW, I found that I was able to do (typing from memory):
>
> class Foo < REXML::Element
> def clone
> klone = self.class.superclass.method( :new ).unbind.bind(
> self.class ).call( self )
> # my stuff here
> klone
> end
> end
>
> ...but I think I'll probably end up re-writing my class to
> wrap/delegate to Element, instead of directly inheriting from it. Pity,
> from an rdoc standpoint.

I have to agree with you. Again and again I use subclassing and it
turns out to be more trouble than it's worth. Delegating always seems
to be a better course. It seems to me it's sort of like duck typing.
Unfortuately delegating means not being able to easily get under the
hood of the "super class", e.g. super and instance variables. I wonder
it some utility methods could help ease that.

T.