On Apr 26, 2011, at 4:10 AM, Aaron wrote:

> @Rob thanks
>
> I thought Hash.new{[]} was shortcut for Hash.new{|h,k|h[k]=[]}
>
>


The block form is executed when the requested key does not exist. It  
makes each call a new array (which is then thrown away).n

irb> answer = [1,4,9].inject(Hash.new {[]}){ |result, i| result[:mem]  
<< i;   result }
=> {}
irb> answer[:mem]
=> []
irb> answer[:foo]
=> []

The non-block form makes a single array the default for all keys. The  
resulting hash still has no entries, but if you ask for any key that  
doesn't exist, you'll get the (one) default value back. That default  
value is a true object that is modified by the << each time the inject  
block is executed.

irb> answer = [1,4,9].inject(Hash.new([])){ |result, i| result[:mem]  
<< i;   result }
=> {}
irb> answer[:mem]
=> [1, 4, 9]
irb> answer[:foo]
=> [1, 4, 9]

Even though your question was for ruby 1.9, the same behavior is found  
in 1.8.

-Rob

Rob Biedenharn		
Rob / AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab / GaslightSoftware.com		http://GaslightSoftware.com/