# Reusing the existing code

**URL:** <https://www.ruby-forum.com/t/reusing-the-existing-code/129768>\
**Category:** Ruby\
**Created:** [February 21, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768 "2008-02-21T12:05:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 21, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/1 "2008-02-21T12:05:48Z")

</div>

Hi!  
I have a new class which needs to reuse the code in two classes.  
But I can only derive it from one class. How should I go about it then?

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 21, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/2 "2008-02-21T12:10:03Z")

</div>

Hi,

On Thu, Feb 21, 2008 at 10:04 PM, MohsinHijazee  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi!  
> I have a new class which needs to reuse the code in two classes.  
> But I can only derive it from one class. How should I go about it then?

Perhaps they should be modules instead? Mix-in with `include' (which makes the module's methods instance methods) or `extend’ (which makes them  
class  
methods).

> > module A  
> > def hi; puts “hi”; end  
> > end  
> > =\> nil  
> > module B  
> > def hey; puts “hey”; end  
> > end  
> > =\> nil  
> > class C  
> > include A  
> > extend B  
> > end  
> > =\> C  
> > C.new.hi  
> > hi  
> > =\> nil  
> > C.hey  
> > hey  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 21, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/3 "2008-02-21T12:16:37Z")

</div>

On 21 æÅ×, 13:02, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi!  
> š šI have a new class which needs to reuse the code in two classes.  
> But I can only derive it from one class. How should I go about it then?

Use mixins, by defining modules

Module A  
def afunc; 5; end  
end

Module B  
def bfunc; 6; end  
end

class AB  
include A  
include B  
end

x AB.new  
x.afunc # → 5  
x.bfunc # → 6

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 21, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/4 "2008-02-21T12:40:03Z")

</div>

Hi,

On Thu, Feb 21, 2008 at 10:24 PM, MohsinHijazee  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The problem is that the second class which I want to use is derived  
> from the ActionController::Base. I cannot make that class into a  
> module otherwise I would be disturbing a the functionality of the  
> Rails Application.

Then, what’s the first one?

Arlen.

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 21, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/5 "2008-02-21T12:25:49Z")

</div>

On Feb 21, 4:10 pm, removed\_email\_address@domain.invalid wrote:

> end  
> x AB.new  
> x.afunc # -\> 5  
> x.bfunc # -\> 6

The problem is that the second class which I want to use is derived  
from the ActionController::Base. I cannot make that class into a  
module otherwise I would be disturbing a the functionality of the  
Rails Application.

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 21, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/6 "2008-02-21T22:36:15Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 21 ???, 13:02, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi!  
> > I have a new class which needs to reuse the code in two classes.  
> > But I can only derive it from one class. How should I go about it then?
> 
> Use mixins, by defining modules

Posting as a beginner, I wonder if something like this (inheriting from  
one class while incorporating an instance of the other) might work:

class SiblingClass \< ParentClass1

def initialize  
@reuseParent2 = ParentClass2.new()  
end

def method\_missing(methodName,\*args)  
@reuseParent2.send(methodName,\*args)  
end

end

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 23, 2008, 4:49am UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/7 "2008-02-23T04:49:12Z")

</div>

That’s \>not\< a beginner posting at all 😉 There’s a well known maxim  
in OO programming to prefer composition (what you are suggesting) to  
inheritance. The Suggestion by prizrak6 can also be used by  
inheriting from one class and mixing in the second bits of functionality  
as a module.  
The thing to always ask yourself is what the relationship between  
the new class and the existing classes is.

New class is an old class -\> inheritance  
New class has an old class -\> composition

Inheritance is very useful but often overused…where composition  
is much more appropriate.

Without knowing more about the actual problem MohsinHijazee has, it’s  
not possible to make detailed recommendations.

Ron F.  
Senior Physicist  
National Superconducting Cyclotron Lab  
Michigan State University  
East Lansing, MI 48824-1321

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 21, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/reusing-the-existing-code/129768/8 "2008-02-21T16:04:42Z")

</div>

> The problem is that the second class which I want to use is derived  
> from the ActionController::Base. I cannot make that class into a  
> module otherwise I would be disturbing a the functionality of the  
> Rails Application.

No, you make a new module, take out the methods from the class which is  
derived from ActionController::Base which you want to reuse, put them in  
the module, then require and include the module in the controller class  
and also your new class. Do the same with the other class (into a  
separate module). You end up with two modules, two existing classes  
which each include their necessary module and your new class includes  
both modules:

module AModule  
end  
class A  
include AModule  
end  
module BModule  
end  
class B \< ActionController::Base  
include BModule  
end

# Then the new class:

class C  
include AModule  
include BModule  
end

Alternately, you just refacter one class, creating one module, and  
include that module in your new class and inherit from the other class:

module AModule  
end  
class A  
include AModule  
end  
class B \< ActionController::Base  
end

# Then the new class:

class C \< B  
include AModule  
end

If you don’t have access to refacter either class, then you can’t do it.

M
