# Why doesn't method\_missing affect respond\_to?

**URL:** <https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481>\
**Category:** Ruby\
**Created:** [June 5, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481 "2006-06-05T13:41:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 5, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481/1 "2006-06-05T13:41:53Z")

</div>

Take this class:

class Foo  
def method\_missing(m, \*args)  
m.to\_s  
end  
end

irb(main):006:0\> Foo.new.foo  
=\> “foo”  
irb(main):007:0\> Foo.respond\_to? :foo  
=\> false

This obviously just returns the name of any method that doesn’t exist  
for the object. So if you can send the object any message and get a  
result, why doesn’t it respond\_to those messages?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 5, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481/2 "2006-06-05T14:21:22Z")

</div>

On Monday 05 June 2006 5:39 am, Pat M. wrote:

> This obviously just returns the name of any method that doesn’t exist  
> for the object. So if you can send the object any message and get a  
> result, why doesn’t it respond\_to those messages?

Think about it from an implementation perspective. How is respond\_to?  
to know  
what method\_missing is going to do?

If you define a method\_missing method, and you need your object’s  
respond\_to?  
to reflect your method\_missing magic, it is up to you to write a  
respond\_to  
method that will do so.

Kirk H.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 5, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481/3 "2006-06-05T14:23:38Z")

</div>

Pat M. wrote:

> Take this class:
> 
> class Foo  
> def method\_missing(m, \*args)  
> m.to\_s  
> end  
> end
> 
> irb(main):006:0\> Foo.new.foo  
> =\> “foo”  
> irb(main):007:0\> Foo.respond\_to? :foo  
> =\> false
> 
> This obviously just returns the name of any method that doesn’t exist  
> for the object. So if you can send the object any message and get a  
> result, why doesn’t it respond\_to those messages?
> 
> Pat

The respond\_to method can’t know what method\_missing is doing with  
missing methods, so it’s the up to the class author to provide a version  
of respond\_to that agrees with method\_missing.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 5, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481/4 "2006-06-05T13:54:11Z")

</div>

Hi –

On Mon, 5 Jun 2006, Pat M. wrote:

> irb(main):007:0\> Foo.respond\_to? :foo  
> =\> false
> 
> This obviously just returns the name of any method that doesn’t exist  
> for the object. So if you can send the object any message and get a  
> result, why doesn’t it respond\_to those messages?

Maybe because that would render #respond\_to? essentially useless 🙂

David

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 5, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481/5 "2006-06-05T16:01:39Z")

</div>

On Jun 5, 2006, at 8:23 AM, Tim H. wrote:

> > =\> “foo”  
> > missing methods, so it’s the up to the class author to provide a  
> > version  
> > of respond\_to that agrees with method\_missing.

More to the point, if you do this:

# foo.rb

class Foo  
def method\_missing(m, \*args)  
eval “def self.#{m}; ‘#{m}’; end”  
self. **send** (m)  
end  
end

**END**

irb(main):001:0\> require ‘foo’  
=\> true  
irb(main):002:0\> f = Foo.new  
=\> #Foo:0x1cd438  
irb(main):003:0\> f.respond\_to? :foo  
=\> false  
irb(main):004:0\> f.foo  
=\> “foo”  
irb(main):005:0\> f.respond\_to? :foo  
=\> true  
irb(main):006:0\>

You’ve now defined a ‘foo’ method and further invocations of f.foo  
will go to the newly minted method rather than method\_missing each time.

-Rob

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 5, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481/6 "2006-06-05T18:02:55Z")

</div>

On 6/5/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > Maybe because that would render #respond\_to? essentially useless 🙂

In my example where the object responds to everything, sure. It was  
just an example though, and of course not every class is going to act  
like this.

I guess the key is to write a predicate and stick it in both methods.

class Foo  
def method\_missing(m, \*args)  
valid\_method?(m) ? m.to\_s : super  
end

def respond\_to?(m)  
valid\_method?(m)  
end

private  
def valid\_method?(m)  
true  
end  
end

Of course it’s not particularly useful here, but you could change  
valid\_method? to  
def valid\_method?(m)  
[:foo, :bar].include? m  
end

Guess I just thought Ruby would automatically pick it up. However, I  
don’t see how it’d be able to, short of creating a copy of the object  
in memory and calling the method on the object to see if it returns a  
result. If the copy gives a method missing error, then respond\_to?  
would return false. Obviously it would suck to create a copy and  
actually call the method just to see if an object responds to it.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 5, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/why-doesnt-method-missing-affect-respond-to/62481/7 "2006-06-05T19:01:38Z")

</div>

Pat M. wrote:

> Guess I just thought Ruby would automatically pick it up. However, I  
> don’t see how it’d be able to, short of creating a copy of the object  
> in memory and calling the method on the object to see if it returns a  
> result. If the copy gives a method missing error, then respond\_to?  
> would return false. Obviously it would suck to create a copy and  
> actually call the method just to see if an object responds to it.
> 
> Pat

Not to mention the problems that could occur when Ruby tries calling a  
method just to see if it works:

class War  
def start(type=:nuclear)  
…  
end  
end

war = War.new  
war.respond\_to? :start
