From: aero6dof@... (Alan Chen) Date: 2003-09-04T04:03:24+09:00 Subject: Re: show me the ruby way "Mark J. Reed" wrote in message news:<20030903145103.GC17861@mulan.thereeds.org>... > In other words, you have it backwards. The return value of the block is > the important thing from the standpoint of the Hash specification; anything > else is just a side effect. Which the autovivification solution presented > earlier in this thread was exploiting. > > -Mark 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 From my standpoint this code should either return 3 3, or 0 0, but not 3 0. If the return value of the block is to be the intended semantic, shouldn't the second line return 3 too? FWIW, I think an additional st_lookup after running the default block for a undefined key access would preserve the intended semantics of both usages of the autovivification block (setting the value by block return, and explicity setting the value) while protecting the programmer from this edge case. Cheers, - alan