From: Chad Perrin Date: 2011-12-06T04:59:26+09:00 Subject: Re: black magical hash element vivification On Tue, Dec 06, 2011 at 01:34:39AM +0900, Adam Prescott wrote: > On Mon, Dec 5, 2011 at 16:28, Chad Perrin wrote: > > > >> foo = Hash.new( Hash.new ) > > => {} > > >> foo[3][2] = true > > => true > > >> foo > > => {} > > > > This is odd. > > > > >> foo[3] > > => {2=>true} > > >> foo[2] > > => {2=>true} > > > > This isn't. Hash.new(Hash.new) != Hash.new { |h, k| h[k] = Hash.new }. The > left hand side uses the same object for each default access, hence f = > Hash.new([]); f[1] << 1; f[2].include?(1) == true. The right hand-side > behaves as you expect for this part. I figured out that Hash.new(Hash.new) used the same object for all of them, but found that quite surprising. Now that I see it side-by-side with the block argument form, though, I realize it should not have surprised me as it did. Thanks for helping me fix my code. > > foo.rehash didn't fix anything. Looks like a bug to me? That's what I was wondering -- whether I'd found a bug in there somewhere.