From: Sean O'Dell Date: 2004-06-06T14:24:06+09:00 Subject: Re: How to ducktype a Hash? On Saturday 05 June 2004 21:36, Austin Ziegler wrote: > > 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. I'm checking for hash-like behavior, because I have no control over the data coming in, and it gets converted to Ruby objectsprior to a certain point in code which expects these objects to be a hash-like object. Right now, that can mean a hash, a custom class that has hash functionality, the Env object, or any other object that gets developed in the future with hash-like functionality. kind_of?, respond_to? and simply duck typing don't help in this regard. My only solution is to implement some sort of tagging method and tag Hash, the custom objects and any objects I can think of, and test for that. That will work, but it feels like a kludge to run around tagging existing Ruby objects with a type id. Sean O'Dell