From: Austin Ziegler Date: 2004-06-06T13:36:17+09:00 Subject: Re: How to ducktype a Hash? Lloyd Zusman: > The way I understand it, duck typing is used at run time to decide how > to operate on a given object. Here's one example out of many: Um ... not the way that I understand it. Duck typing, per se, isn't necessarily at all about testing an object's class, type, or interface. It is simply assuming that an interface is there on the provided object(s) and using it in a particular way. If it doesn't work, then someone screwed up. You could do some interface checking to see if an object claims to have a particular method (what Sean O'Dell is calling duck typing), but that is something that can never really be trusted, in any language. (Maybe Eiffel.) IMO, Mr O'Dell needs to consider why he is checking for a "Hash-like" behaviour. As Nobu (?) suggested, this is more likely indicated by #each_pair than anything else (Struct behaves that way, although OpenStruct does not -- perhaps an oversight). After all, Procs implement #[] -- but they aren't Array *or* Hash (and this is because there is -- thankfully -- no () operator in Ruby). I see no problem with checking for #kind_of?(String), or Array, or Hash -- if you must absolutely have the semantics offered by those classes, since they are fundamental. However, doing so will limit your code rather severely, and there are alternatives in most cases. -austin