Declaring a Hash as a Constant with a default value in Ruby in one line
<p>My goal is to use a Hash with a default value as a class constant. To do this it seems to me that I must initialise a Hash in one line as a constant with pre-defined value and a default value.</p>

<p>According to the Ruby documentation, I can set a default value in two way :</p>

<ul>
<li><p>In the constructor then by adding value as follow :</p>

<pre><code>MY_HASH = Hash.new(-1)
MY_HASH[1] = 0
MY_HASH[2] = 42
</code></pre></li>
<li><p>By adding values first then setting the default value later :</p>

<pre><code>MY_HASH = {
    1=&gt;0,
    2=&gt;42,
}
MY_HASH.default = -1
</code></pre></li>
</ul>

<p>I tried to do it in one line like this, but it does not work :</p>

<pre><code>    MY_HASH = {
        1=&gt;0,
        2=&gt;42,
    }.default = -1

   #It's the same as :
   MY_HASH = -1
</code></pre>

<p>Is there a way to declare a Hash with a default value in one line ?</p>