# Duplication from a Subclass, Object#become

**URL:** <https://www.ruby-forum.com/t/duplication-from-a-subclass-object-become/68436>\
**Category:** Ruby\
**Created:** [September 12, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/duplication-from-a-subclass-object-become/68436 "2006-09-12T18:17:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 12, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/duplication-from-a-subclass-object-become/68436/1 "2006-09-12T18:17:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 12, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/duplication-from-a-subclass-object-become/68436/2 "2006-09-12T18:31:27Z")

</div>

> > > > > “G” == Gavin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
