我想知道class method_missing是定义在哪个类中的。 它是在Object类中定义的。
我该如何找出沿着继承层次结构,哪个类覆盖了它?
我想知道class method_missing是定义在哪个类中的。 它是在Object类中定义的。
我该如何找出沿着继承层次结构,哪个类覆盖了它?
您可以使用UnboundMethod#owner
方法来检查该方法的实现位置:
class A
def method_missing(*args)
# do something
end
end
method = A.instance_method(:method_missing)
method.owner
# => A
owner
将返回该模块。
method
的值,它显示为#<UnboundMethod: A#method_missing>
,显示了所有者A
。同样地,在class B<A; end; class C<B; end
中,C.instance_method(:method_missing) => #<UnboundMethod: C(A)#method_missing>
,再次显示A
作为所有者。正确吗? - Cary Swoveland.owner
就能看到所有者是谁。 - Cary Swovelandowner
可以让人清楚地理解,所以我认为我的答案不需要修改。 - Marek Lipka