# Inheritance with Ruby

**URL:** <https://www.ruby-forum.com/t/inheritance-with-ruby/131099>\
**Category:** Ruby\
**Created:** [March 6, 2008, 1:19am UTC](https://www.ruby-forum.com/t/inheritance-with-ruby/131099 "2008-03-06T01:19:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [March 6, 2008, 1:19am UTC](https://www.ruby-forum.com/t/inheritance-with-ruby/131099/1 "2008-03-06T01:19:20Z")

</div>

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

class Sub  
def new  
#do something second  
end  
end

class Super \< Sub  
def new  
#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

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [March 6, 2008, 2:56am UTC](https://www.ruby-forum.com/t/inheritance-with-ruby/131099/2 "2008-03-06T02:56:17Z")

</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()  
#do something second  
end  
end

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

/Shawn

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [March 6, 2008, 3:59am UTC](https://www.ruby-forum.com/t/inheritance-with-ruby/131099/3 "2008-03-06T03:59:02Z")

</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 P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [March 6, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/inheritance-with-ruby/131099/4 "2008-03-06T18:16:58Z")

</div>

## ---------------------------------------------------------- Object#extend obj.extend(module, …) =\> obj

```
 Adds to _obj_ the instance methods from each module given as a
 parameter.

    module Mod
      def hello
        "Hello from Mod.\n"
      end
    end

    class Klass
      def hello
        "Hello from Klass.\n"
      end
    end

    k = Klass.new
    k.hello #=> "Hello from Klass.\n"
    k.extend(Mod) #=> #<Klass:0x401b3bc8>
    k.hello #=> "Hello from Mod.\n"

```

Regards

Maria

On 2008-03-06 01:19:14 +0100, Chirag P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [March 6, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/inheritance-with-ruby/131099/5 "2008-03-06T16:57:38Z")

</div>

On Wed, Mar 5, 2008 at 4:19 PM, Chirag P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def new  
> #do something first  
> 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.
