Robert Klemme
8/8/2008 7:45:00 AM
2008/8/8 Chuck Remes <cremes.devlist@mac.com>:
>
> 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