title: Sum values in array of hash if they have the same value
body: <p>I saw this piece of code in this post, because I'm trying to sum values in an array of hashes based on some criteria.</p>
  
  <p><a href="https://stackoverflow.com/questions/43087297/rails-sum-values-in-an-array-of-hashes">Rails sum values in an array of hashes</a></p>
  
  <pre><code>array = [
    {loading: 10, avg: 15, total: 25 },
    {loading: 20, avg: 20, total: 40 },
    {loading: 30, avg: 25, total: 55 }
  ]
  
  sum = Hash.new(0)
  
  array.each_with_object(sum) do |hash, sum|
    hash.each { |key, value| sum[key] += value }
  end
   # =&gt; {:loading=&gt;60, :avg=&gt;60, :total=&gt;120}
  </code></pre>
  
  <p>What I'm trying to do and I don't know how, is to sum total key if loading and avg appear with the same values more than once in this array. For instance.</p>
  
  <pre><code> array = [
        {loading: 10, avg: 15, total: 25 },
        {loading: 20, avg: 20, total: 40 }, # See here
        {loading: 30, avg: 25, total: 55 },
        {loading: 20, avg: 20, total: 80 }, # See here
        {loading: 10, avg: 20, total: 46 }
      ]
  </code></pre>
  
  <p>The result would be:</p>
  
  <pre><code>[
    {loading: 10, avg: 15, total: 25 },
    {loading: 20, avg: 20, total: 120 }, # Results in this
    {loading: 30, avg: 25, total: 55 },
    {loading: 10, avg: 20, total: 46 }
  ]
  </code></pre>
  
  <p>I tried to modify this line </p>
  
  <pre><code>hash.each { |key, value| sum[key] += value }
  </code></pre>
  
  <p>Adding a conditional that checks if the value is repeated but I didn't succeed. </p>
  
  <p>Any help, ideas or anything will be welcome.</p>