假设这里有一些我不知道的任意库代码:
- 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(其中x引用了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
可怕?华丽?
解决方法@H_301_18@
你可以这样做:
- if x.method(:hi).owner == Foo
我远不是Ruby专家;如果有人比这更好,我不会感到惊讶.
- if x.method(:hi).owner == Foo
我远不是Ruby专家;如果有人比这更好,我不会感到惊讶.