From: elbows@... (Nathan Weston) Date: 2004-06-06T11:38:40+09:00 Subject: Re: How to ducktype a Hash? How about using the fetch and store methods? These work the same as Hash#[] and Hash#[]=, but are unique to Hash. Your other hash-like objects might not implement them, but they would be easy to add. Nathan "Sean O'Dell" wrote in message news:<200406041715.09691.sean@celsoft.com>... > 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.). > > My problem is, respond_to?(:[]) returns true for objects which are array-like > and for strings (both have [] methods). I need to prevent strings, arrays, > and any other object which responds to :[] from getting to this one point in > code, and only allow a hash or hash-like object. The data type at this > particular point in the code comes from a variety of data sources, and there > are lots of ways it could become a string, or an array or theoretically any > object. I need to know when it's a hash or hash-like object, and raise an > exception when it is not. My biggest problem right now is, when it's a > string or an array, the code has no way of knowing if the returned object/nil > is a valid value, or if a "undesirable" object has slipped in and returned > what it likes to return when you call its [] method. > > So, how do I positively identify when an object is a hash or at least behaves > like a hash? > > Sean O'Dell