문제

So, in Ruby, if I have a class hierarchy that looks something like (very symbolically)

A < B < C < Object

and have a method "foo" defined in class C, is there some bit of introspection that allows me to determine that C is the class defining the method that responds to A.new.foo?

도움이 되었습니까?

해결책

Yes... do A.new.method(:foo).owner. It will give you class C. Read #owner.

다른 팁

Method#owner is purpose-built for this, but this could also be used:

A.ancestors.find { |c| c.instance_methods(false).include? :foo }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top