From: Joel VanderWerf Date: 2004-06-06T11:08:45+09:00 Subject: Re: How to ducktype a Hash? Lloyd Zusman wrote: > Duck typing cannot work unless methods with the same names and > signatures behave in the same way in the various classes that implement > them. If the ability to do duck typing is deemed to be an important and > necessary feature of an object-oriented language, then the standard > libraries that are written for that language _must_ provide different > names and/or signatures for methods that perform different functions. But "same" and "different" are on a continuum. Sometimes Array#[] and Hash#[] are close enough that sharing their method name makes sense (e.g., using hashes as sparse arrays). > Since [] and []= have certain semantics for one set of classes > (array-like classes) and different semantics for other classes > (hash-like classes), then we must conclude that Ruby was not designed > with the duck typing in mind. [] and []= don't even have the same semantics from hash to hash, because of default_procs, singletons, etc. An object may respond to all the usual Hash methods, and the object may be a Hash in the sense of obj.is_a?(Hash), but it still doesn't behave as expected. Of course, this may be an extreme example that is irrelevant to a given application. But it reinforces the point that it's really hard to encode semantics within interface, in any language. Hence unit tests.