Within a method at runtime, is there a way to know if that method has been called via super
in a subclass? E.g.
module SuperDetector
def via_s
There is probably a better way, but the general idea is that Object#instance_of?
is restricted only to the current class, rather than the hierarchy:
module SuperDetector
def self.included(clazz)
clazz.send(:define_method, :via_super?) do
!self.instance_of?(clazz)
end
end
end
class Foo
include SuperDetector
def bar
via_super? ? 'super!' : 'nothing special'
end
end
class Fu < Foo
def bar
super
end
end
Foo.new.bar # => "nothing special"
Fu.new.bar # => "super!"
super
in the child. If the child has no such method and the parent's one is used, via_super?
will still return true
. I don't think there is a way to catch only the super
case other than inspecting the stack trace or the code itself.