You can use the first argument of the Hash.new
method for that:
d = Hash.new 0
d[:new_key] += 1
d[:new_key] #=> 1
d[:foo] #=> 0
Be careful - you might accidentally change the default value:
h = Hash.new("Go Fish")
h[:unknown_key] #=> "Go Fish"
h[:unknown_key].upcase! #=> "GO FISH"
h[:next_key] #=> "GO FISH"
As "mu is too short" pointed out in his answer, you should better use a proc, as in:
h = Hash.new { |h, k| h[k] = 0 }