假设这里有一些我不知道的任意库代码:
class Foo
def hi
end
end
class Bar < Foo
def hi
end
end
假设我有一些代码Bar
作为参数传递。
def check(x)
do_something_with(x.method(:hi))
end
在上面的例子中,我能知道x.hi
(where x
references an instance of Bar
) 与 不同Foo#hi
吗?
根据加雷斯的回答,这就是我到目前为止所得到的:
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
可怕?华丽的?