From: Mauricio Fernandez Date: 2007-01-11T04:05:22+09:00 Subject: Re: a most undangerous Hash#store! On Tue, Jan 09, 2007 at 10:46:20AM +0900, Trans wrote: > > Mauricio Fernandez wrote: > > > Anyway, Trans was talking about destructive updates, so what about > > > > def store?(key, value) > > unless key?(key) > > store(key,value) > > return true > > end > > false > > end > > > > similar to the #store! he described, but with a more regular behavior when you > > have nil/false values... > > well, i was thinking of combining Mr. Haines idea with Mr. James to > get #store_new, which seemed just about right. but now your suggestion > is quite interesting. at first i wasn't so sure, but thinking about it > a little more it makes sense -- "is something stored with this key? if > not store this value with it". i was also worried that it did not > return the stored value like the normal store, but i realize now that > could be ambigious if value=nil, so you may be on point. is that right? Yes, that's the idea ("more regular behavior"), but as Robert said, a predicate method with side-effects seems _very_ dangerous (yes, store!? would be more accurate :). So it might make sense to name it e.g. #store_once, with the above semantics. Returning the value that was passed to the method makes little sense: it doesn't provide any information we didn't have to being with. -- Mauricio Fernandez - http://eigenclass.org - singular Ruby