Zhang Kai Yu
Zhang Kai Yu

Reputation: 361

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?

Upvotes: 3

Views: 163

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51171

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.

Upvotes: 6

Related Questions