Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return nil when break the injection block earilier

Tags:

ruby

I want to sum up the orders amount.

but if order.upgrade != nil I will end the block instantly.

But I got the nil value.

That wasn't my expectation.

How to achieve my goal?

Thanks

points = orders.inject(0) do |sum, order |
  break if order.upgrade != nil      
  sum += order.actual_amount if order.actual_amount
end
like image 513
newBike Avatar asked Dec 12 '13 13:12

newBike


1 Answers

points = orders.inject(0) do |sum, order |
  break sum unless order.upgrade.nil?      
  sum += order.actual_amount || 0
end
like image 114
BroiSatse Avatar answered Oct 14 '22 23:10

BroiSatse