From: Tim Bates Date: 2003-08-09T08:48:10+09:00 Subject: Re: Ducktype, right? On Sat, Aug 09, 2003 at 06:46:26AM +0900, Dave Thomas wrote: > In general, if a Ruby programmer finds themselves using respond_to? too > much, I'd ask them 'why?'. > > If they're switching on the result, then I'd recommend looking at > polymorphic solutions, or possibly double dispatch. > > If they're checking parameters, I'd suggest commenting out the check and > seeing what happens. :) This is something like my approach to Duck Typing. The documentation of my library says that this method parameter expects a Duck *or similar*. The actual code does no checking of whether it's a Duck or a Giraffe, or even if it can quack; it just asks it to quack. If I'm feeling particularly zealous, I even document what methods of Duck I actually use. Then, it's up to the user of my library: if they pass in a Goose, and it can quack, good for them. If they pass in a Mouse, they can expect things to break. It's probably not the most robust of coding styles, but I write my libraries for intelligent people (ie me), not to be foolproof. Of course, I perform lots of sanity checking on my end-user input. ;) Tim Bates -- tim@bates.id.au