From: Sean O'Dell Date: 2004-06-06T02:24:12+09:00 Subject: Re: How to ducktype a Hash? On Friday 04 June 2004 22:03, Steven@ruby-lang.org, Grady@ruby-lang.org, From@ruby-lang.org: wrote: > >On Sat, 5 Jun 2004, Sean O'Dell wrote: > > I need to detect when an object is a hash-like container for other > > objects, one which responds to [] and which can index using any object as > > a value (string, etc.). > > You have two requirements: that it respond to [], and that it can > index any object. I think you can check these both at run-time with: > def isHash(o) > begin o[Object.new] rescue return false end > return true > end That's a little bit expensive. I'm sure it works, but way too much overhead for just a type check. Sean O'Dell