Reusing the existing code.

M

MohsinHijazee

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?
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Hi,

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).
hi
=> nilhey
=> nil
 
P

prizrak6

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
 
M

MohsinHijazee

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

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.
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Hi,

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.
 
M

Mark Bush

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
 
R

Ron Fox

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 Fox
Senior Physicist
National Superconducting Cyclotron Lab
Michigan State University
East Lansing, MI 48824-1321
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top