From: Brian Candler Date: 2003-09-03T23:25:52+09:00 Subject: Re: show me the ruby way On Wed, Sep 03, 2003 at 10:54:09AM +0900, Alan Chen wrote: > Hmm, I meant h[k] not h[h] as I originally posted below... > > md = Hash.new { |h,k| h[k] = 0; 3 } > # access undefined key 1 > p md[1] # => 3 # returns the block result > p md[1] # => 0 # now we discover the actual default value > > It's clear to me exactly what is happening, but I guess my thought was > that this behavior is a bug. If the code that runs the autovivication > block can recognize that the value for the undefined key was set, then > perhaps the block return value should be ignored. I guess it would > require an additional st_lookup after the initial lookup fails. Doesn't really seem to be that useful a test to me. If you want to write code which says "as a side-effect set the stored value to 0, but the value to return to the user is 3" then why not :-) Regards, Brian.