Or square bracket method

Hash[1,2,3,4] # => {1 => 2, 3 => 4}

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 05/02/2009, at 1:27 AM, Martin DeMello <martindemello / gmail.com>  
wrote:

> On Wed, Feb 4, 2009 at 12:04 AM, Stuart Clarke
> <stuart.clarke1986 / gmail.com> wrote:
>>
>>       counts = Hash.new(0)
>>       eventdateID.each {|d| counts[d] += 1}
>
> Here is your problem. Hash.new(0) means "when I query the hash, and
> the key I request is not in there, return 0". It does not actually add
> {key => 0} to the hash itself. To do that, you need the block form of
> Hash.new, which yields as block the hash itself and the key:
>
> counts = Hash.new {|h, k| h[k] = 0}
>
> irb(main):001:0> a = Hash.new(0)
> => {}
> irb(main):002:0> b = Hash.new {|h,k| h[k] = 0}
> => {}
> irb(main):003:0> a['hello']
> => 0
> irb(main):004:0> b['hello']
> => 0
> irb(main):005:0> a
> => {}
> irb(main):006:0> b
> => {"hello"=>0}
>
> martin
>