In Ruby, how can I programmatically determine which class / module defines the method that is being called?
Say in a given scope, I call some_method() . In the same scope, I would like to call the find_method(:some_method) function, which will return a Class class, Singleton Class, or Module that defines some_method .
Here is some code to illustrate what I mean:
class Foo include ... def bar ... some_method()
I assume that I need to use a complex mixture of reflective functions, starting with self and using self.ancestors , to approach the inheritance tree using method_defined? to check if a method is defined in the class or Module and, possibly, some other tricks for checking areas from the most internal to the external (since the code can work inside, for example, instance_eval ).
I just donβt know the correct order and all the subtleties of the Ruby metamodel for implementing find_method so that it is exhaustive in its search and corrected in terms of allowing method find_method .
thanks!
source share