Fernando Perez | 15 May 21:46

Re: Contionnal sum

Thank you all for your messages. Yes it is a piece of code I am using in 
a Rails app. I thought it would fit better in this list rather than the 
Rails one, as the problem is not specific to Rails.

> BTW: Comparing a boolean value to true or false only shows you don't
understand booleans.

Yeah I knew someone would nail me on this. I wanted to make my problem 
clear, so that's why I specified this silly "== true". In my real code, 
I don't have it trust me. If I hadn't put it, someone would have come up 
with "hey wtf is that order.paid thing!?"

> The .select method is a good way, but you can use less memory by doing
> 
> @orders.sum {|order| order.paid? order.amount : 0 }

This is fantastic, I knew the solution was super simple. Day after day I 
love Ruby more and more.

Best regards, and once again thank you very much for assisting me.

--

-- 
Posted via http://www.ruby-forum.com/.


Gmane