# Method\_missing behaves differently when a method is called v

**URL:** <https://www.ruby-forum.com/t/method-missing-behaves-differently-when-a-method-is-called-v/75554>\
**Category:** Ruby\
**Created:** [November 10, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/method-missing-behaves-differently-when-a-method-is-called-v/75554 "2006-11-10T23:09:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [November 10, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/method-missing-behaves-differently-when-a-method-is-called-v/75554/1 "2006-11-10T23:09:42Z")

</div>

For certain methods, it seems to behave differently if called via  
reflection. Consider the following example:

class Foo  
def method\_missing(method, \*args, &block)  
“#{method} called”  
end  
end

f = Foo.new

f.bar(‘a’)

-\> “bar called”

f.send(:bar, ‘a’)

-\> “bar called”

f.open(‘a’)

-\> “open called”

f.send(:open, ‘a’)

Errno::ENOENT: No such file or directory - a  
from (irb):8:in `initialize’  
from (irb):8

* * *

I know that ‘open’ is a Kernel method, but I would have expected  
f.open(‘a’) and f.send(:open, ‘a’) to behave similarly - either call the  
Kernel method or call method\_missing. Does anyone know why they’re  
different?

Thanks,

Brian H.

* * *

This email may contain confidential material.  
If you were not an intended recipient,  
Please notify the sender and delete all copies.  
We may monitor email to and from our network.

* * *

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [November 10, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/method-missing-behaves-differently-when-a-method-is-called-v/75554/2 "2006-11-10T23:17:43Z")

</div>

On 11/10/06, Hartin, Brian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> → “open called”  
> f.open(‘a’) and f.send(:open, ‘a’) to behave similarly - either call the  
> Kernel method or call method\_missing. Does anyone know why they’re  
> different?

All Kernel methods are private. Usually, a Kernel method is called in  
procedural style (without a receiver) and since every object includes  
Kernel, the private method is visible and called. But when there is an  
explicit receiver (“receiver.method\_name”), private methods are left  
out of the lookup. So when you call f.open, the private Kernel#open is  
masked and the unhandled method is passed on to method\_missing.  
Object#send, however, bypasses privacy restrictions so f.send(:open)  
catches hold of Kernel#open and you get the error.

So, in short, it’s a matter of: private w/ send vs. private w/o send

Jacob F.
