Here is a simple Python program that outputs changes in value of consecutive elements:
amounts = [10, 9, 10, 3, 100, 100, 90, 80, 10, 30, 10]
for i in range(0, len(amounts)-1):
if amounts[i+1] > amounts[i]:
print("up", amounts[i+1]-amounts[i])
elif amounts[i+1] < amounts[i]:
print("down", amounts[i]-amounts[i+1])
else:
print("stay")
The following Ruby code is my first attempt at translating the above from Python:
amounts = [10, 9, 10, 3, 100, 100, 90, 80, 10, 30, 10]
for i in 0..(amounts.count)-1 do
if amounts.at(i+1) > amounts.at(i)
printf "up %d", (amounts.at(i+1)-amounts.at(i))
if amounts.at(i+1) < amounts.at(i)
printf "down %d", (amounts.at(i)-amounts.at(i+1))
end
else
print "stay"
end
end
This Ruby code returns NoMethodError as it is. If anyone could enlighten me as to the magical ways of Ruby, I would be very pleased.