From: Robert Klemme Date: 2008-08-08T16:45:03+09:00 Subject: Re: Most compact command for associate array 'totalling'? 2008/8/8 Chuck Remes : > > On Aug 7, 2008, at 5:27 PM, Siep Korteling wrote: > >> Chuck Remes wrote: >>> >>> On Aug 7, 2008, at 4:59 PM, John Pritchard-williams wrote: >>> a = Hash.new { |h,k| h[k] = 0 } >>> a["abc"] += 1 >> a = Hash.new(0) >> a["abc"] += 1 >> >> Has the same effect. > > Quite true, but I find the block form to be a better "general case" form to > know since it is so much more flexible. But that's just my preference. That's true in a way - although for this particular case I'd prefer the solution with the default parameter. This can be slightly more efficient at times because it does not insert a value on each query to the Hash. For other frequent cases, I use the block form. This is a typical (for me) application: items = Hash.new {|h,k| h[k] = []} ... items[item.category] << item Kind regards robert -- use.inject do |as, often| as.you_can - without end