title: Ruby aggregation with objects
body: <p>Lets say I have something like this:</p>
  
  <pre><code>class FruitCount
     attr_accessor :name, :count
  
     def initialize(name, count)
       @name = name
       @count = count
     end
   end
  
   obj1 = FruitCount.new('Apple', 32)
   obj2 = FruitCount.new('Orange', 5)
   obj3 = FruitCount.new('Orange', 3)
   obj4 = FruitCount.new('Kiwi', 15)
   obj5 = FruitCount.new('Kiwi', 1)
  
   fruit_counts = [obj1, obj2, obj3, obj4, obj5]
  </code></pre>
  
  <p>Now what I need, is a function <code>build_fruit_summary</code> which due to a given <code>fruit_counts</code> array, it returns the following summary:</p>
  
  <pre><code>fruits_summary = {
    fruits: [
      { 
        name: 'Apple',
        count: 32
      },
      {
        name: 'Orange',
        count: 8
      },
      {
        name: 'Kiwi',
        count: 16
      }
    ],
    total: {
      name: 'AllFruits',
      count: 56
    }
  }
  </code></pre>
  
  <p>I just cannot figure out the best way to do the aggregations.</p>
  
  <p>Edit:</p>
  
  <p>In my example I have more than one count. </p>
  
  <pre><code>class FruitCount
     attr_accessor :name, :count1, :count2
  
     def initialize(name, count1, count2)
       @name = name
       @count1 = count1
       @count2 = count2
     end
   end
  </code></pre>