I have to calculate returns between each elements of an array
Please see a sample of it:
nav = ["100.00", "86.21", "84.65", "82.46", "86.94"]
and here’s the loop I used:
0.upto(nav.count - 2) do |i|
perf = (nav[i + 1].to_f / nav[i].to_f - 1) * 100
p perf
end
It works, but my point is all about the last element of the array.
First I wanted to use something like:
nav.length.times do
...
end
Then, I used:
0.upto(nav.count - 2)
in order to avoid the calculation of the (last value + 1) which would be nil.
However, I would be pleased to know if there is a better way to do it.
Thanks for your suggestion.
and if you need to return an array of values: