foo = [10,20,30,40,50,60]
foo_total = 0
foo.each do |f|
print foo_total += f
end
print foo_total
The output of this code sample will be 10 30 60 100 150 210 210 but I'm actually looking for 10 20 30 40 50 60 210
I know that it can be achieved by using the following code
foo = [10,20,30,40,50,60]
foo_total = 0
foo.each do |f|
print f
foo_total += f
end
print foo_total
However, is there a more elegant approach that needs just one line in the foo.each block?
edit: It's not only about the output, but also about having the variable foo_total that contains the sum of all values in foo
print (foo_total += f) - f.print f; foo_total +=fwould be one line, but not one statement.