From: Dan Doel Date: 2003-09-13T18:57:25+09:00 Subject: Re: Duck Typing Meino Christian Cramer wrote: > If both works: is_a? and respond_to? : What is the advance of one > over the other beside the fact, that one has a cool name : "Duck > typing"? > > Is one slower, less clean or ... ? I am just curious... > > You really shouldn't use #is_a? or #respond_to? much at all. That's not what duck typing is about. Just write your method to use methods that the passed in object should have, by the contract of the method. Then test to make sure your program works. Excessively using #is_a? and #respond_to? is essentially trying to turn ruby from a dynamically typed language into an ad-hoc statically typed language, which isn't in the spirit of duck typing at all. The only real reason you should be using these methods is if you need to take different actions depending on which of several methods is available. If you're just checking whether the only method you use exists or not, you're just doing extra work most of the time. The interpreter will tell you if it isn't there. It can be a confusing issue if you haven't read the discussions before. There was a big one some weeks back, so it might be worth it to read the ruby-talk archives to clear some stuff up without duplicating all of it again. Cheers. - Dan