I have a class Parent and a class Child (which is a child subclass of Parent).
Parenthas a method calledaaaa.Childhas a method calledbbbb.
This is what I want to achieve:
- I want
bbbbto be an extension ofaaaa. If I callaaaaon aChildobject, it will run whataaaawould normally do, plus whatever else inbbbb. - Calling
bbbbwill do the same as above (runs whataaaawould 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?