FlurryWInd
FlurryWInd

Reputation: 305

Where is the method_missing defined in ruby

I use ruby-1.9.3.

When the method_missing method came, I try to find out where this method was defined. I take a look at Ruby Doc, and find that the method was defined in BasicObject, But when I use BasicObject.methods.grep /^method/ in irb, It gave me a result array without any method_missing method, Then, I try Kernel.methods.grep /^method/, and still no method_missing method there.

Can you Help me? Where can I find this method?

Upvotes: 3

Views: 356

Answers (4)

Thyago B. Rodrigues
Thyago B. Rodrigues

Reputation: 630

In case anyone else is reading some old materials and get confused, this method apparently used to belong to Kernel module until v1.8.7.330. Now, as mentioned, it's been moved to BasicObject.

http://apidock.com/ruby/Kernel/method_missing

Upvotes: 1

Patrick Oscity
Patrick Oscity

Reputation: 54674

If you want to see the source, you'll have to dig around in C code (for MRI) e.g. with gem install pry pry-doc you can do

~$ pry
[1] pry(main)> show-source method_missing

From: vm_eval.c (C Method):
Owner: BasicObject
Visibility: private
Number of lines: 7

static VALUE
rb_method_missing(int argc, const VALUE *argv, VALUE obj)
{
    rb_thread_t *th = GET_THREAD();
    raise_method_missing(th, argc, argv, obj, th->method_missing_reason);
    UNREACHABLE;
}

Upvotes: 2

Arup Rakshit
Arup Rakshit

Reputation: 118261

Use Method#owner to know which method is defined in which class.

method(:method_missing).owner # => BasicObject

Upvotes: 8

p.matsinopoulos
p.matsinopoulos

Reputation: 7810

It's a private method: Try:

BasicObject.private_methods.grep /missing/

Upvotes: 7

Related Questions