# Inheritance with Ruby

**URL:** <https://rubytalk.org/t/inheritance-with-ruby/44993>\
**Category:** ruby-talk\
**Created:** [6 March 2008 00:19 UTC](https://rubytalk.org/t/inheritance-with-ruby/44993 "2008-03-06T00:19:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chirag\_Patel1](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Chirag\_Patel1](https://rubytalk.org/u/Chirag_Patel1)\
**Post date:** [6 March 2008 00:19 UTC](https://rubytalk.org/t/inheritance-with-ruby/44993/1 "2008-03-06T00:19:14Z")

</div>

I would like to create a new superclass 'Super' for an existing class  
'Sub'

class Sub  
&nbsp;&nbsp;def new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something second  
&nbsp;&nbsp;end  
end

class Super \< Sub  
&nbsp;&nbsp;def new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something first  
&nbsp;&nbsp;end  
end

Is it possible create this new Super class so that Super::new is called  
every time Sub:new is called without modifying Sub?

The is because Sub already exists and I don't want to tamper the code.  
Basically I have a bunch of existing classes that I want to inherit from  
'Super' so that I can include some error handling in 'Super'. Is there  
another better way to accomplish this without modifying the existing  
subclasses?

Thanks!  
Chirag

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Shawn\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@Shawn\_Anderson](https://rubytalk.org/u/Shawn_Anderson)\
**Post date:** [6 March 2008 01:55 UTC](https://rubytalk.org/t/inheritance-with-ruby/44993/2 "2008-03-06T01:55:23Z")

</div>

Keep in mind that in Ruby you do not override new. You use initialize.

To call your parent's constructor you would call super from the child's  
initialize method.

class Sub  
def initialize()  
&nbsp;&nbsp;&nbsp;&nbsp;#do something second  
end  
end

class Super \< Sub  
def initialize()  
&nbsp;&nbsp;&nbsp;&nbsp;#do something first  
&nbsp;&nbsp;&nbsp;&nbsp;super  
end  
end

/Shawn

> **···**
>
> On Wed, Mar 5, 2008 at 4:19 PM, Chirag Patel \<patelc75@yahoo.com\> wrote:
> 
> > I would like to create a new superclass 'Super' for an existing class  
> > 'Sub'
> > 
> > class Sub  
> > def new  
> > &nbsp;&nbsp;&nbsp;&nbsp;#do something second  
> > end  
> > end
> > 
> > class Super \< Sub  
> > def new  
> > &nbsp;&nbsp;&nbsp;&nbsp;#do something first  
> > end  
> > end
> > 
> > Is it possible create this new Super class so that Super::new is called  
> > every time Sub:new is called without modifying Sub?
> > 
> > The is because Sub already exists and I don't want to tamper the code.  
> > Basically I have a bunch of existing classes that I want to inherit from  
> > 'Super' so that I can include some error handling in 'Super'. Is there  
> > another better way to accomplish this without modifying the existing  
> > subclasses?
> > 
> > Thanks!  
> > Chirag  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Thomas\_Mueller](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@Thomas\_Mueller](https://rubytalk.org/u/Thomas_Mueller)\
**Post date:** [6 March 2008 02:58 UTC](https://rubytalk.org/t/inheritance-with-ruby/44993/3 "2008-03-06T02:58:00Z")

</div>

I recently wanted to add a few methods to existing classes and just  
extended Object. Not sure if that would help.  
You could maybe override Object.initialize, but I'm not so sure  
whether that would be a smart idea...

> **···**
>
> 2008/3/6, Chirag Patel \<patelc75@yahoo.com\>:
> 
> > I would like to create a new superclass 'Super' for an existing class  
> > 'Sub'
> > 
> > class Sub  
> > &nbsp;&nbsp;def new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something second  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Super \< Sub  
> > &nbsp;&nbsp;def new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something first  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Is it possible create this new Super class so that Super::new is called  
> > every time Sub:new is called without modifying Sub?
> > 
> > The is because Sub already exists and I don't want to tamper the code.  
> > Basically I have a bunch of existing classes that I want to inherit from  
> > 'Super' so that I can include some error handling in 'Super'. Is there  
> > another better way to accomplish this without modifying the existing  
> > subclasses?
> > 
> > Thanks!  
> > Chirag
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Christopher\_Dicely](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Christopher\_Dicely](https://rubytalk.org/u/Christopher_Dicely)\
**Post date:** [6 March 2008 15:56 UTC](https://rubytalk.org/t/inheritance-with-ruby/44993/4 "2008-03-06T15:56:38Z")

</div>

> I would like to create a new superclass 'Super' for an existing class  
> 'Sub'
> 
> class Sub  
> &nbsp;&nbsp;def new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something second  
> &nbsp;&nbsp;end  
> end
> 
> class Super \< Sub  
> &nbsp;&nbsp;def new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something first  
> &nbsp;&nbsp;end  
> end

Note that this makes Sub a superclass of Super (Super inherits from  
Sub), not the  
other way around. I don't know if you can change the superclass of a class at  
runtime, because Ruby uses single inheritance and Sub already has an parent  
(Object). You can make a module, but AFAIK not a class even though a class is a  
module, a "superclass" by including it.

> Is it possible create this new Super class so that Super::new is called  
> every time Sub:new is called without modifying Sub?

No, but since Ruby has open classes, its pretty easy to modify sub so  
that Super::initialize (what you probably really want) is called whenever  
Sub::initialize is called (either before or after Sub::initialize executes).

> The is because Sub already exists and I don't want to tamper the code.

You don't have to modify the code of that you are getting Sub from to  
modify its behavior.

> Basically I have a bunch of existing classes that I want to inherit from  
> 'Super' so that I can include some error handling in 'Super'. Is there  
> another better way to accomplish this without modifying the existing  
> subclasses?

The best way is probably to create a module that does what you want,  
and reopen the existing classes and include that module in each of them.

> **···**
>
> On Wed, Mar 5, 2008 at 4:19 PM, Chirag Patel \<patelc75@yahoo.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Maria\_Almar](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@Maria\_Almar](https://rubytalk.org/u/Maria_Almar)\
**Post date:** [6 March 2008 17:15 UTC](https://rubytalk.org/t/inheritance-with-ruby/44993/5 "2008-03-06T17:15:05Z")

</div>

---------------------------------------------------------- Object#extend  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.extend(module, ...) =\> obj

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Adds to \_obj\_ the instance methods from each module given as a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module Mod  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def hello  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Hello from Mod.\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Klass  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def hello  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Hello from Klass.\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k = Klass.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k.hello #=\> "Hello from Klass.\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k.extend(Mod) #=\> #\<Klass:0x401b3bc8\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k.hello #=\> "Hello from Mod.\n"
> 
> Regards
> 
> Maria
> 
> On 2008-03-06 01:19:14 +0100, Chirag Patel \<patelc75@yahoo.com\> said:
> 
> > I would like to create a new superclass 'Super' for an existing class  
> > 'Sub'
> > 
> > class Sub  
> > &nbsp;&nbsp;def new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something second  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Super \< Sub  
> > &nbsp;&nbsp;def new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do something first  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Is it possible create this new Super class so that Super::new is called  
> > every time Sub:new is called without modifying Sub?
> > 
> > The is because Sub already exists and I don't want to tamper the code.  
> > Basically I have a bunch of existing classes that I want to inherit from  
> > 'Super' so that I can include some error handling in 'Super'. Is there  
> > another better way to accomplish this without modifying the existing  
> > subclasses?
> > 
> > Thanks!  
> > Chirag
