title: What's the Ruby equivalent of Python's defaultdict?
body: <p>In Python, I can make a hash where each element has a default value when it's first referenced (also know as "autovivification"). Here's an example:</p>
  
  <pre><code>from collections import defaultdict
  d = defaultdict(int)
  d["new_key"] += 1
  print d
  </code></pre>
  
  <p>Printing the dict shows the value for "new_key" is 1.</p>
  
  <p>What's the equivalent in Ruby? This code throws an error:</p>
  
  <pre><code>d = {}
  d[:new_key] += 1
  puts d
  
  test.rb:3:in `&lt;main&gt;': undefined method `+' for nil:NilClass (NoMethodError)
  </code></pre>