title: Sum of particular values in a Ruby hash
body: <p>I have two hashes:</p>
  
  <pre><code>first = { 1 =&gt; [15, 15, 15, 8], 4 =&gt; [11, 12, 7, 7], 5 =&gt; [14, 17, 13, 13],
            6 =&gt; [19, 19, 15, 15], 7 =&gt; [5, 12, 12, 12], 8 =&gt; [10, 14, 14, 14], 
            9 =&gt; [8, 7, 8, 8] } 
  
  second = { 1 =&gt; [0, 1, 2], 4 =&gt; [2, 3], 5 =&gt; [2, 3], 6 =&gt; [0, 1, 2, 3], 
             7 =&gt; [1, 2, 3], 8 =&gt; [1, 2, 3], 9 =&gt; [2, 3] }
  </code></pre>
  
  <p>As you see they both use same keys, but values are different. <code>second</code>'s values contains ranges of indexes of the <code>first</code>'s values and I need to sum only those <code>first</code>'s values that are in those ranges. </p>
  
  <p>The expected output:</p>
  
  <pre><code>result = { 1 =&gt; [45], 4 =&gt; [14], 5 =&gt; [26], etc }
  </code></pre>