Yes... do A.new.method(:foo).owner
. It will give you class C
. Read #owner
.
In Ruby, is there a way to find the class that responds to a method call?
-
13-07-2023 - |
Question
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
?
Solution
OTHER TIPS
Method#owner is purpose-built for this, but this could also be used:
A.ancestors.find { |c| c.instance_methods(false).include? :foo }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow