[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Duplication from a Subclass, Object#become

Gavin Kistner

9/12/2006 4:17:00 PM

I know that this has been discussed before, but searching has not turned
up the definitive answers I had hoped for.

I'm stuck in a situation like this:

class Foo
def dup
# strange, magical, important things happen here
# which know about all of Foo's instance variables
end
end

class Bar < Foo
def dup
super
end
end

b1 = Bar.new
b2 = b2.dup
p b2.class #=> Foo

How do I get around it? Is subclassing inherently a problem here, and I
need to wrap-and-delegate to a foo instance held by my bar instance?

Will evil.rb ever be part of a standard Ruby distribution, so I could
do:
class Bar < Foo
def dup
klone = super
klone.class = Bar
klone
end
end


1 Answer

ts

9/12/2006 4:31:00 PM

0

>>>>> "G" == Gavin Kistner <gavin.kistner@anark.com> writes:

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 ?

G> end


Guy Decoux