Phillip Gawlowski | 15 May 18:41

Re: Contionnal sum


Fernando Perez wrote:
| Hi,
|
| I am wondering if it is possible to create a conditionnal sum.
|
| For instance I would like to do something like this:
|
| @total_amount = @orders.sum { |order| order.amount if order.paid == true
| }

Should be.

Something like this (untested, and unsafe, probably not even correct):

def conditional_sum order, condition
~  order.each do |o|
~    sum = sum + o unless o.paid?
~  end
~  return sum
end

I made the idea explicit, so that you should at least get the idea.

--
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

You! What PLANET is this!
~ -- McCoy, "The City on the Edge of Forever", stardate 3134.0

Gmane