From: jmichel@... (Jean Michel) Date: 2001-01-08T06:40:02+09:00 Subject: [ruby-talk:8787] Re: bug?? In article <200101062231.RAA02774@yashi.com>, Yasushi Shoji wrote: ... >Hash#default and its accessors were added to prevent error with code >like: > >hash = {} >... >hash["foo"] += 1 > >after the change so that nil + 1 yields error. [ruby-list:26602] > >so having two ways (default and key_missing) to fix the same thing >isn't good IMHO. The problem is that the chosen implementation of Hash#default is not good, I think. It would be perfect if a.default="" (or a.default=[]) was equivalent to def a.key_missing(f) a[f]="" end but it is not, because the last initializes entries to a *new* empty string each time, while the other does not. This is the trouble I had: after a.default="" a[1]+="a" a[2]+="b" a is {1=>"ab", 2=>"ab"}, not {1=>"a", 2=>"b"} as it should be. Internally Hash should call dup on a.default when creating a new entry to get the desired behaviour. Best regards, Jean MICHEL