Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail From: David Sprague Newsgroups: comp.lang.ruby Subject: hash of arrays Date: Tue, 12 Apr 2011 20:06:20 -0500 Organization: Service de news de lacave.net Lines: 35 Message-ID: <17ed4aba89dcdda100deb6592ec4f9f5@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1302656955 99172 65.111.164.187 (13 Apr 2011 01:09:15 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Wed, 13 Apr 2011 01:09:15 +0000 (UTC) X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381384 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <17ed4aba89dcdda100deb6592ec4f9f5@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2715 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/.