From: Alex Young Date: 2007-07-04T18:13:43+09:00 Subject: Re: Isn't hash default value behavior just a little bizarre? Farhad Farzaneh wrote: > Pe単a, Botp wrote: > >> ruby allows one to change the default value. that is good, imho. >> i think the surprise stems fr the fact that it is _too simple to change >> the default. hey, it's ruby ;) >> >> kind regards -botp > > You're right. Somehow I missed that in the documentation.... did RTFM, > but obviously not carefully enough. > > Still, I do find it pretty surprising. I got stung when I was actually > using the feature to initialize any entry to an array just to avoid the > business of > > hash[key] ||= [] > Apologies if you've already come across this, but I think the correct way to do what you're trying to do is this: h = Hash.new(){|h,k| h[k] = []} The block passed to Hash.new gets called for each new entry in the hash, so a new array will be constructed each time. Hope this helps, -- Alex