title: Ruby: summing up values within an array of hashes between certain dates
body: <p>I have an array of hashes in Ruby:</p>
  
  <pre><code>array = [
    {:date =&gt; Wed, 04 May 2011 00:00:00 PDT -07:00,
     :value =&gt; 200}
    {:date =&gt; Wed, 04 May 2011 01:00:00 PDT -07:00,
     :value =&gt; 100}
    {:date =&gt; Tue, 03 May 2011 01:00:00 PDT -07:00,
     :value =&gt; 300}
    {:date =&gt; Tue, 03 May 2011 01:00:00 PDT -07:00,
     :value =&gt; 150}
  ]
  </code></pre>
  
  <p>I'd like to be able to combine the values within each day so that I have a new array like this:</p>
  
  <pre><code>array = [
    {:date =&gt; Wed, 04 May 2011 00:00:00 PDT -07:00,
     :value =&gt; 300}
    {:date =&gt; Tue, 03 May 2011 00:00:00 PDT -07:00,
     :value =&gt; 450}
  ]
  </code></pre>
  
  <p>What's the most elegant way to search the array by day and sum up the values for each day?</p>
  
  <p>This is what I initially tried:</p>
  
  <pre><code>entries = [
    {:date =&gt; Wed, 04 May 2011 00:00:00 PDT -07:00,
     :value =&gt; 200}
    {:date =&gt; Wed, 04 May 2011 01:00:00 PDT -07:00,
     :value =&gt; 100}
    {:date =&gt; Tue, 03 May 2011 01:00:00 PDT -07:00,
     :value =&gt; 300}
    {:date =&gt; Tue, 03 May 2011 01:00:00 PDT -07:00,
     :value =&gt; 150}
  ]
  
  first_day = 29.days.ago.beginning_of_day
  total_days = 30
  
  day_totals = (0...total_days).inject([]) do |array, num|
      startat = first_day + num.day
      endat = startat.end_of_day
  
      total_value_in_day = entries.where("date &gt;= ? and date &lt;= ?", startat, endat).sum(:value)
  
      array &lt;&lt; {:date =&gt; startat, :value =&gt; total_value_in_day}
  end
  </code></pre>
  
  <p>I realized my mistake was with the <code>where</code> method which is a Rails method for searching objects, and can't be used on arrays. So my main question, is there a way to search arrays or hashes with conditions.</p>