# Is it possible to change the superclass of one class dynamic

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-change-the-superclass-of-one-class-dynamic/144277>\
**Category:** Ruby\
**Created:** [July 28, 2008, 5:47am UTC](https://www.ruby-forum.com/t/is-it-possible-to-change-the-superclass-of-one-class-dynamic/144277 "2008-07-28T05:47:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sean-lerner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@sean-lerner](https://www.ruby-forum.com/u/sean-lerner)\
**Post date:** [July 28, 2008, 5:47am UTC](https://www.ruby-forum.com/t/is-it-possible-to-change-the-superclass-of-one-class-dynamic/144277/1 "2008-07-28T05:47:21Z")

</div>

I want to change the super class of one class in one of plugins, for  
example,  
in plugin  
class A \< B

end  
in rails initialization configuration file,  
class A \< C

end

but when i did this, the error: “TypeError: superclass mismatch”  
appreared.

So can we change the super class of one class dynamically?

Thanks.

---

<div class="post-metadata">

**Author:** ![sean-lerner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@sean-lerner](https://www.ruby-forum.com/u/sean-lerner)\
**Post date:** [July 28, 2008, 6:14am UTC](https://www.ruby-forum.com/t/is-it-possible-to-change-the-superclass-of-one-class-dynamic/144277/2 "2008-07-28T06:14:45Z")

</div>

On Jul 27, 9:47 pm, Sean L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but when i did this, the error: “TypeError: superclass mismatch”  
> appreared.
> 
> So can we change the super class of one class dynamically?
> 
> ## Thanks.
> 
> Posted viahttp://www.ruby-forum.com/.

No. That is not possible. It is, however, possible to include (and  
extend) modules into a class to change its method and constant lookup  
hierarchy.

What is it you’re actually trying to accomplish by changing the  
superclass?

Chris

---

<div class="post-metadata">

**Author:** ![sean-lerner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@sean-lerner](https://www.ruby-forum.com/u/sean-lerner)\
**Post date:** [July 28, 2008, 6:50am UTC](https://www.ruby-forum.com/t/is-it-possible-to-change-the-superclass-of-one-class-dynamic/144277/3 "2008-07-28T06:50:10Z")

</div>

Chris S. wrote:

> On Jul 27, 9:47ï¿½pm, Sean L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > but when i did this, the error: “TypeError: superclass mismatch”  
> > appreared.
> > 
> > So can we change the super class of one class dynamically?
> > 
> > ## Thanks.
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> No. That is not possible. It is, however, possible to include (and  
> extend) modules into a class to change its method and constant lookup  
> hierarchy.
> 
> What is it you’re actually trying to accomplish by changing the  
> superclass?
> 
> Chris

Thanks for quick reply, Chris.  
I just want to change the behavior of one plugin without changing it’s  
source code. I think changing the original source code of that plugin  
isn’t too bad if we can’t change the super class dynamically.

---

<div class="post-metadata">

**Author:** ![sean-lerner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@sean-lerner](https://www.ruby-forum.com/u/sean-lerner)\
**Post date:** [July 28, 2008, 11:40am UTC](https://www.ruby-forum.com/t/is-it-possible-to-change-the-superclass-of-one-class-dynamic/144277/4 "2008-07-28T11:40:33Z")

</div>

On Mon, Jul 28, 2008 at 6:49 AM, Sean L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Posted viahttp://www.ruby-forum.com/.  
> > > Thanks for quick reply, Chris.  
> > > I just want to change the behavior of one plugin without changing it’s  
> > > source code. I think changing the original source code of that plugin  
> > > isn’t too bad if we can’t change the super class dynamically.  
> > > I am afraid it is, if you want to change the behavior imposed by the  
> > > super class, you should change it in the  
> > > subclass, not the superclass. Are there any technical reasons impeding  
> > > this?

## Cheers Robe rt

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

There’s no one thing that’s true. It’s all true.
