From: gabriele renzi Date: 2004-06-05T19:33:40+09:00 Subject: Re: How to ducktype a Hash? il Sat, 5 Jun 2004 04:59:27 +0000 (UTC), Steven Grady From: ha scritto:: >>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 > >Does this meet your needs? you don't need the begin..end, a method block is an "exception container" by itself: def isHash(o) o[Object.new] true rescue false end