From: Jeff Mitchell Date: 2004-06-06T21:33:15+09:00 Subject: Re: How to ducktype a Hash? --- Robert Klemme wrote: > > "gabriele renzi" schrieb im Newsbeitrag > news:t116c09e5964u321qbahvkdorlilg7j1hp@4ax.com... > > il Sun, 6 Jun 2004 18:52:44 +0900, Jeff Mitchell > > ha scritto:: > > > > > > >This is the fault of PStore or of the lack of agreed-upon semantics > > >for classes with hash-like behavior. There should be a standard list > > >which includes the definition of such behavior. Here, I'll start the > > >list now: > > > > > > hash-like: #fetch, #store > > >array-like: #at, #store > > > > > >Any others? How about tree-like? > > > > > >I want this list to be part of the ruby standard -- clearly > > >documented so that implementers know they should define #fetch > > >and #store for the hash-like classes they write. [] should > > >be viewed as convenience only. > > > > I may be wrong but what you're asking has been throw out many times. > > It would be wonderful to have stuff like this for everything, > > toghether with a possible mix-in. > > We have, actually, many 'default quacks' like :read and :write for > > IOs, :each for enumerables, and so on. > > > > Maybe adding some more mix-ins would be fine for many people's need > > (say, if a Map mixin existed, we could have it rely on fetch(),keys() > > and store() to provide all the other methods). This way hash-like > > stuff like PStore, will just need to include the mix-in and Sean could > > just check for kind_of? Map if he feels the need for this. > > > > Oh, and can I ask for an Input and Output mixin, while I'm at it ? :) > > Just a note: excessive need for type checks like these may be a hint for > weak OO design. It is not necessary to introduce a Map mixin, nor would I even suggest it. I would certainly not advocate checking kind_of? Map. Instead, one blindly calls #fetch and #store and then relies on ruby to raise an exception when a method doesn't exist. This is nothing new; nobody says kind_of? Enumerable (I hope) -- we just call #each and see what happens. I would prefer to stick with "hash-like" and leave it at that. The above list I proposed has nothing to do with OO design per se, just convention about what consitutes "hash-like" (and is NOT tied to a particular mixin, necessarily). __________________________________ Do you Yahoo!? Friends. Fun. Try the all-new Yahoo! Messenger. http://messenger.yahoo.com/