From: Roger Braun Date: 2011-04-13T10:27:54+09:00 Subject: Re: hash of arrays Hi David 2011/4/13 David Sprague : > I'm wrote this code to bin a list of words by word-length: > > dict = Hash.new([]) > > dict_file.each do |line| >  line.chomp!() >  dict[line.length] << line > end > > expecting that I could avoid testing each time whether this was a new > entry in the hash or not by just appending to the default, an empty > array, > if it is new. See http://ruby-doc.org/core/classes/Hash.html#M000718. > > What happens is that the *same* array is assigned as the default value > to all new entries so that all the hash entries finish with the same > array of values. > > is there away to void having to write something like: > > if dict.key?(line.length) >  dict[line.length] << line > else >  dict[line.length] = line > end dict = Hash.new{Array.new} should work. -- Roger Braun rbraun.net | humoralpathologie.de