Can I detect that a method has been overridden?

Viewed 1357

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?

2 Answers
Related