From: Jeff Mitchell Date: 2004-06-06T17:10:57+09:00 Subject: Re: How to ducktype a Hash? --- Sean O'Dell wrote: > On Saturday 05 June 2004 15:01, John W. Long wrote: > > >why do you need to say 'it is hash like' ? > > >Wo'nt it be easyer to say 'I need this methods, so I'll just check > > >them' ? > > > > > >BTW, probably the simpler solution is : > > > > > >x.methods =={}.methods > > > > This is even worse than object.kind_of(Hash). The whole point of "duck > > typing" (as I understand it) is that you don't check if an object has > > certain methods. You just use them. Ruby will throw an error if there's > > a problem. > > Which always sounded fine to me until I was actually exposed to this kind of > "typing." The problem is, the methods [] and []= are there, they just don't > do what I need them to do. Duck typing completely and utterly don't help, > and Ruby has no way to tell me when an object's [] and []= are hash-like or > something else. My solution is going to end being something like "tagging > all objects that I know in advance to be hash-like, and then looking for that > tag at that particular point in my code." That's a kludge to me. Why should you expect [] to imply hash-like behavior in the first place? It's just a convenience method. What was the problem with the #lookup and #store suggestion at [ruby-talk:102505]? Honestly I don't see the issue here. The criteria you've chosen for a duck is: "makes a light sound while walking". Well, tons of animals do that. You need distinct "QUACK"s. Those quacks are #store and #lookup. __________________________________ Do you Yahoo!? Friends. Fun. Try the all-new Yahoo! Messenger. http://messenger.yahoo.com/