From: Sean O'Dell Date: 2004-06-08T01:44:16+09:00 Subject: Re: How to ducktype a Hash? On Monday 07 June 2004 08:39, Paul Sanchez wrote: > Richard Kilmer wrote: > > > > 2) Sean: It seems like what you are looking for is to check a method for > > 'semantic equivalence'...that is, you want to say that the [] and []= > > methods on an object are semantically equivalent to the [] and []= > > methods on Hash. Not that the object is in any way (from and inheritance > > perspective) related to a Hash, nor does it actually get the behavior of > > a Hash object's [] and []= methods, but when it comes to those two > > methods...'quack like a Hash'. Does that sound right? If so, the > > question becomes how to express this kind of semantic equivalence both in > > the definition of the methods, and the checking with respond_to? > > I've been watching this thread and I think you've hit the nail on the > head. Sean seems to want semantic equivalence, but is using a syntax > which is overloaded out of the box. Duck typing is based on syntax > matching. It seems to me that the only way to get semantic equivalence > without using type cues or explicit testing is to steer clear of > overloaded operators and methods, so that syntax reflects semantics. > This is what several others were suggesting when they talked about using > hash's alternate method names in place [] and []=. I'm not sure what this means. I'm not looking for duck typing per se, just a way to type check hash-like objects. The only two things I care about when checking for something in this way are that the object responds to a group of methods which all take the same number of parameter, and the group of methods represent a described interface; that is, a known functionality. In this particular case, the functionality should be that of a hash, or a hash-like object. Sean O'Dell