From: Robert Dober Date: 2007-05-31T05:31:50+09:00 Subject: Re: #respond_to? not working for dynamically generated metho On 5/30/07, James Edward Gray II wrote: > On May 30, 2007, at 8:34 AM, Robert Dober wrote: > > > On 5/30/07, James Edward Gray II wrote: > >> On May 30, 2007, at 7:53 AM, dblack@wobblini.net wrote: > >> > >> > You could of course shoehorn find_by_* into respond_to? for AR > >> > objects, if you don't mind, essentially, writing method_missing > >> twice > >> > (once for real, once as a kind of pseudo-static twin). > >> > >> I don't really understand this stance. My opinion is that providing > >> a method_missing() implementation is a convenient way for a > >> programmer to define a lot of dynamic methods. This increases the > >> messages an object responds to. > > > > I have always felt that dynamic method creation and dynamic message > > interception are not quite the same beast. But I guess you have some > > thoughts behind your claim of which I fail to grasp the concept. Would > > you mind to elaborate? > > I'm not really sure what else to say. I read the documentation for > respond_to?(): > > $ ri -T Object#respond_to? > ----------------------------------------------------- Object#respond_to? > obj.respond_to?(symbol, include_private=false) => true or false > ------------------------------------------------------------------------ > Returns +true+> if _obj_ responds to the given method. Private > methods are included in the search only if the optional second > parameter evaluates to +true+. > > I know that ActiveRecord model classes will except find_by_*() > methods, so the fact that respond_to?() returns false for them makes > the above documentation lie. James the documentation is not correct anyway, look at the wording, nothing ever can reply to methods, only to messages. I liked a lot what David has said about this. >That does not seem good to me and it's > certainly possible to fix it. Thus, I can only reason that it is a > bug in ActiveRecord. That is kind of a thing I have not considered in my reply, I misunderstood that you were dreaming about a magical Object#respond_to? which would interpret the semantics of #method_missing generally, hence my surprise. > > > Is this really a good idea? > > Is it a good idea not to? What exactly is the use case for > respond_to?()? I have only ever used it to find out if something I > want is available. If I can't do that reliably, it doesn't really > help me much. Hmm sure but I feel its need more in MetaProgramming, if you want to fix ActiveRecord I cannot argue with you at all I do not know that stuff, so I just bail out of the discussion. However if you would like to spend some time in philosophy about respond_to? I see a clearcut dilemma between the ideal behavior of respond_to? and method_missing. See my example above but it is easy to imagine that #method_missing is dispatching depending on an external datasource. How would you want #respond_to? to handle this? I just feel that for practical reasons #respond_to? does a good job already. This might Robert -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw