From: Robert Dober Date: 2007-05-30T20:50:50+09:00 Subject: Re: #respond_to? not working for dynamically generated methods 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 ;) ? For what concerns intercepted messages for which no methods exist David has said it all below:... > > 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) > > Cheers Robert -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw