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.

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

or the ternary equivalent in the inner loop?

thanks,

Dave

-- 
Posted via http://www.ruby-forum.com/.