# Redefine a class with same name

**URL:** <https://www.ruby-forum.com/t/redefine-a-class-with-same-name/131401>\
**Category:** Ruby\
**Created:** [March 9, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/redefine-a-class-with-same-name/131401 "2008-03-09T12:02:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 9, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/redefine-a-class-with-same-name/131401/1 "2008-03-09T12:02:14Z")

</div>

Is there any way to to remove the definition of a class so I can reuse  
its  
name? Like below I’d like to reuse the name ‘A’ but since it’s already  
defined with a different superclass I get a ‘TypeError: superclass  
mismatch  
for class A’.

* * *

class X  
end  
class Y  
end

class A \< X  
end

==\> Here I’d like to remove A’s definition so I can:

class A \< Y  
end

==\> without getting a: ‘TypeError: superclass mismatch for class A’

* * *

Thanks,  
Martin

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 9, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/redefine-a-class-with-same-name/131401/2 "2008-03-09T13:30:39Z")

</div>

> > > > > “M” == Martin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

M\> Is there any way to to remove the definition of a class so I can  
reuse its  
M\> name? Like below I’d like to reuse the name ‘A’ but since it’s  
already  
M\> defined with a different superclass I get a ‘TypeError: superclass  
mismatch  
M\> for class A’.

Just remove the constant

M\> class A \< X  
M\> end

Object.send(:remove\_const, :A)

M\> class A \< Y  
M\> end

Guy Decoux

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 9, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/redefine-a-class-with-same-name/131401/3 "2008-03-09T16:16:08Z")

</div>

Excellent! That’s fine for me. I only have the case for a web framework  
of  
mine when using with fastcgi as different calls might use same class  
names  
which I can unregister now.

Thanks,  
martin

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 9, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/redefine-a-class-with-same-name/131401/4 "2008-03-09T13:51:47Z")

</div>

On 3/9/08, ts [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> M\> end
> 
> Object.send(:remove\_const, :A)
> 
> M\> class A \< Y  
> M\> end

Keep in mind though, that this only removes the binding between the  
constant and the class, it doesn’t actually allow you to change the  
old class, and any existing instances will still be instances of the  
old class not the new one:

k$ irb  
irb(main):001:0\> class A  
irb(main):002:1\> def foo  
irb(main):003:2\> end  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> a = A.new  
=\> #\<A:0x6e578\>  
irb(main):006:0\> a.foo  
=\> nil  
irb(main):007:0\> Object.send(:remove\_const, :A)  
=\> A  
irb(main):008:0\> a.foo  
=\> nil  
irb(main):009:0\> a.class  
=\> A  
irb(main):010:0\> A  
NameError: uninitialized constant A  
from (irb):10  
irb(main):011:0\> class A  
irb(main):012:1\> def bar  
irb(main):013:2\> end  
irb(main):014:1\> end  
=\> nil  
irb(main):015:0\> a.bar  
NoMethodError: undefined method `bar’ for #\<A:0x6e578\>  
from (irb):15  
irb(main):016:0\>

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
