Hi --

On Wed, 30 May 2007, Maurice Gladwell wrote:

> It seems Object#respond_to doesn't work for dynamically generated
> methods:
>
> SomeRailsModel.respond_to? 'find_by_name' #=> false
>
> 1. Is there some way to *really* check if an object will respond to a
> message, even if it will respond by a dynamically generated method?
>
> Right now, Object#respond_to? just doesn't do what it name promises: it
> doesn't check if an object responds to a message :foo, only if the
> object has a :foo method defined.

That's what responding to a method means.  Otherwise, it would be
meaningless, since you can send any message to any object.

Things like find_by_name are made possible by method_missing, which
intercepts uninterpretable messages.  It's possible for method_missing
to generate a new method dynamically, but it doesn't have to (and in
fact "find_by_name" does not get generated as a method; it just gets
examined as a string).  Even if it does, there's no way for respond_to
to figure it out.

Basically, to know what's going to happen dynamically, you have to run
the program and send the messages.


David

-- 
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)