From: "Jesús Gabriel y Galán" Date: 2009-02-05T00:18:31+09:00 Subject: Re: Hash counting On Wed, Feb 4, 2009 at 3:27 PM, Martin DeMello wrote: > On Wed, Feb 4, 2009 at 12:04 AM, Stuart Clarke > 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. This is true, but counts[d] += 1 is actually counts[d] = counts[d] + 1 so the RHS will evaluate to 1 the first time, assigning it to the hash: irb(main):001:0> h = Hash.new(0) => {} irb(main):002:0> h["a"] += 1 => 1 irb(main):003:0> h => {"a"=>1} So the above snippet is correct for generating a histogram. Jesus.