3

我想知道定义了哪个类 method_missing 。它在对象中定义。

如何确定层次结构中的哪个类覆盖它?

4

1 回答 1

6

您可以使用UnboundMethod#owner方法来检查方法的实现位置:

class A
  def method_missing(*args)
    # do something
  end
end
method = A.instance_method(:method_missing)
method.owner
# => A

注意:如果方法在模块中实现(后来混入类层次结构的某个地方),owner将返回这个模块。

于 2014-11-25T08:24:14.287 回答