I have a class Parent
and a class Child
(which is a child subclass of Parent
).
Parent
has a method calledaaaa
.Child
has a method calledbbbb
.
This is what I want to achieve:
- I want
bbbb
to be an extension ofaaaa
. If I callaaaa
on aChild
object, it will run whataaaa
would normally do, plus whatever else inbbbb
. - Calling
bbbb
will do the same as above (runs whataaaa
would normally do and then do whatever else is inbbbb
).
This is what I ended up doing:
class Parent
def aaaa
print "A"
end
end
class Child < Parent
alias_method :original_aaaa,:aaaa
def bbbb
original_aaaa
print "B"
end
alias_method :aaaa,:bbbb
end
c = Child.new
c.aaaa # => AB
c.bbbb # => AB
It works. I guess. But it felt really hackish. Plus, a disadvantage of this is that if I wanted to extend aaaa
with the same name either before or after defining bbbb
, things get a bit strange.
Is there a simpler way to achieve this?