Inheritance with Ruby

C

Chirag Patel

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
 
S

Shawn Anderson

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

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
 
T

Thomas Mueller

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

Christopher Dicely

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

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

Maria Almar

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

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

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top