From: Gavin Sinclair Date: 2002-10-09T21:16:58+09:00 Subject: Re: strange Hash default behaviour From: "George Ogata" > > This seems similar to the problem with Array.new: > > Array.new(5, []) will put the same "[]" in each cell, so: > > a = Array.new(5, []) > a[2] << 3 > a # [[3],[3],[3],[3],[3]] > > If you're using 1.7?+, you can give a block to the constructor which is > evaluated to get the value of each cell: > > a = Array.new(5) {[]} > a[2] << 3 > a # [[],[],[3],[],[]] Very nice. When initialising an array-of-arrays like that, I usually do (I'm not usually a 1.7 user): arr = (1..N).map { [] } > A similar thing seems to exist for Hash: > [...] Gavin