title: Ruby Hash Initializers
body: <p>Hash initializers:</p>
  
  <pre><code># this
  animals = Hash.new { [] }
  animals[:dogs] &lt;&lt; :Scooby
  animals[:dogs] &lt;&lt; :Scrappy
  animals[:dogs] &lt;&lt; :DynoMutt
  animals[:squirrels] &lt;&lt; :Rocket
  animals[:squirrels] &lt;&lt; :Secret
  animals #=&gt; {}
  # is not the same as this
  animals = Hash.new { |_animals, type| _animals[type] = [] }
  animals[:dogs] &lt;&lt; :Scooby
  animals[:dogs] &lt;&lt; :Scrappy
  animals[:dogs] &lt;&lt; :DynoMutt
  animals[:squirrels] &lt;&lt; :Rocket
  animals[:squirrels] &lt;&lt; :Secret
  animals #=&gt; {:squirrels=&gt;[:Rocket, :Secret], :dogs=&gt;[:Scooby, :Scrappy, :DynoMutt]}
  </code></pre>
  
  <p>I saw someone post these on another question, but I don't understand why animals appears blank in the first case. If I type </p>
  
  <pre><code>animals[:dogs]
  </code></pre>
  
  <p>I get the appropriate array.</p>