Duplication from a Subclass, Object#become

G

Gavin Kistner

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 =3D Bar.new
b2 =3D b2.dup
p b2.class #=3D> 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 =3D super
klone.class =3D Bar
klone
end
end
 
T

ts

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top