rubyclassmetaprogramminginstancemethod-missing

Ruby. How to know which class instance method is defined?


I want to know which class method_missing is defined. It is defined in Object.

How can I figure out which class along the hierarchy overrides it?


Solution

  • You can use UnboundMethod#owner method to check where the method is implemented:

    class A
      def method_missing(*args)
        # do something
      end
    end
    method = A.instance_method(:method_missing)
    method.owner
    # => A
    

    Note: If the method is implemented in module (which is later mixed into the class hierarchy somewhere), owner will return this module.