I'm new to Rails (and ruby). What is the standard way of iterating through an array to total a variable.
e.g. for the total expenses in a month, first an Array:
expenses_this_month = expenses.find :all,
:conditions => ['date >= ? and date <= ?',
Date.today.beginning_of_month, Date.today.end_of_month]
I already of know of two ways of doing it:
total = 0.0
for expense in expenses_this_month
total += expense.cost
end
return total
or with a block
total = 0.0
expenses_this_month.each do |expense|
total += expense.cost
end
return total
I'm aware that the last line in a ruby method will be returned by default, so there must be a better way of writing this?