Suppose here's some arbitrary library code that I don't know about:
class Foo
def hi
end
end
class Bar < Foo
def hi
end
end
And suppose I have some code where I'm passed Bar
as a parameter.
def check(x)
do_something_with(x.method(:hi))
end
In the above example, can I know that x.hi
(where x
references an instance of Bar
) is different from Foo#hi
?
Based on Gareth's answer, this is what I've got so far:
def is_overridden?(method)
name = method.name.to_sym
return false if !method.owner.superclass.method_defined?(name)
method.owner != method.owner.superclass.instance_method(name).owner
end
Hideous? Gorgeous?
You could do this:
if x.method(:hi).owner == Foo
I am far from being a Ruby expert; I will not be surprised if someone has a better way than this.
Interesting question! I wondered about the same question. You can reopen Bar class and check who are the ancestors in the lookup path of Bar that have the method defined.
class Bar < Foo
ancestors.each do |ancestor|
puts ancestor if ancestor.instance_methods.include?(:hi)
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With