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.

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.

> 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.

It would be interesting to see an implementation of James Edward Gray's 
suggestion above. ("You use an Extract Method refactoring to pull out
the matching logic into a separate private method and use that in
both implementations.")

--
M.

-- 
Posted via http://www.ruby-forum.com/.