From: dblack@... Date: 2007-05-30T20:55:39+09:00 Subject: Re: #respond_to? not working for dynamically generated methods Hi -- On Wed, 30 May 2007, Robert Dober wrote: > On 5/30/07, dblack@wobblini.net wrote: >> 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. > Does not look right though > > # vim: sts=2 sw=2 expandtab nu tw=0: > > > module M > def m; "m" end > end > class A > def a; "a" end > end > > class B < A > include M > end > > b = B.new > def b.b; "b" end > > p B.new.respond_to?(:a) > p B.new.respond_to?(:m) > p B.method_defined?(:a) > p B.method_defined?(:m) > p b.respond_to?(:b) > true > true > true > true > true > > looks pretty good to me. > Is Rails fooling around with my beloved Ruby ;) ? No, it's just using method_missing, which intercepts the "find_by_name" message. > For what concerns intercepted messages for which no methods exist > David has said it all below:... I'll go along with that :-) 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)