假设这里有一些我不知道的任意库代码:
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吗?
根据Gareth的回答,这是我到目前为止所得到的:
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
可怕?华丽?
Gar*_*han 12
你可以这样做:
if x.method(:hi).owner == Foo
我不是一个Ruby专家; 如果有人有比这更好的方法,我不会感到惊讶.
| 归档时间: | 
 | 
| 查看次数: | 857 次 | 
| 最近记录: |