From: dblack@... Date: 2007-05-31T00:13:32+09:00 Subject: Re: #respond_to? not working for dynamically generated metho Hi -- On Wed, 30 May 2007, Maurice Gladwell wrote: > David wrote: >> If it's absolutely known what messages every object will respond to, >> then we don't need #respond_to? :-) > > #respond_to? is usually used when we don't know exactly which object is > referenced by a particular name, and especially what it can do ("duck > typing"). So we check if `foo.respond_to? :to_s` to see if we can tell > it to convert itself to a String. #respond_to? != duck typing, though. At least "hard" duck typing is just: foo.to_s > But if we do know that foo is, say, an Array, we should - as the system > programmers - know which messages it will respond_to and how. It's > generally never the case that we know which object we have, yet are not > sure which messages it will respond to. Well... there you get into the issue of class vs. type. If you have a spare six months, you can read the archives of this list on that topic :-) But certainly in ActiveRecord you get objects (association collections) that will tell you that they are Arrays but which have many methods beyond the vanilla Array.new object. While you're right that my rand(2) example is contrived, I'm not ready to say that it's so clear that method_missing can never be used to handle cases that we couldn't anticipate and enumerate. That seems like a kind of retro-constraint, just so that it will interoperate with respond_to?. >> I don't know... it's obviously >> just a slightly different way of looking at it. I'm uneasy with >> having to rewrite respond_to? every time I implement method_missing; >> it seems like a lot of work and an awfully tight coupling. > > Yes, that is true. Everyone would be happy (some much happier, others a > tiny bit happier :-) if we had a really working #respond_to? that would > cover all cases. But implementing such a thing elegantly seems > non-trivial. I consider #respond_to? to be working, but I know what you mean :-) 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)