class Base
def sam
"I m the base"
end
def self.inherited(base)
alias_method :old_sam, :sam
base.class_eval do
def sam
old_sam
p "Inside inherited"
end
end
super
end
end
class Derived < Base
def sam
p "Inside Derived"
end
end
when Derived.new.sam => "Inside Derived"
But I expect
"Inside Derived"
"Inside inherited"
New to ruby. Any help will be greatly appreciated.
You have simply overriden the defined by
base.class_eval
methodsam
inDerived
.If you remove the method
sam
fromDerived
:You will get:
The latter is because you are passing an argument to
old_sam
method, which does not take it:This is not possible with your set up, because what you do is first defining an instance method
sam
inclass_eval
block for all descending classes, but later just overriding in.