Reusing the existing code

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?

Hi,

On Thu, Feb 21, 2008 at 10:04 PM, MohsinHijazee
[email protected]
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

On 21 æÅ×, 13:02, MohsinHijazee [email protected] 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

Hi,

On Thu, Feb 21, 2008 at 10:24 PM, MohsinHijazee
[email protected]
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.

On Feb 21, 4:10 pm, [email protected] 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.

[email protected] wrote:

On 21 ???, 13:02, MohsinHijazee [email protected] 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

That’s >not< a beginner posting at all :wink: 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

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