15 May 18:41
Re: Contionnal sum
From: Phillip Gawlowski <cmdjackryan <at> googlemail.com>
Subject: Re: Contionnal sum
Newsgroups: gmane.comp.lang.ruby.general
Date: 2008-05-15 16:41:48 GMT
Subject: Re: Contionnal sum
Newsgroups: gmane.comp.lang.ruby.general
Date: 2008-05-15 16:41:48 GMT
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
RSS Feed