I have a loop, and in it want to assign a string to the value of i + (i-1) but am not sure how to progress.
(1..50).each do |i|
answer = "#{i+(i-1)}"
end
In this code the answer must be a string as it will eventually relate to a database table.
What is the best way of evaluating this within the string, I have tried a few variations of this but haven't had any luck so any helpful pointers would be much appreciated.
eval. Right now this sounds like an XY Problem and you're asking about Y instead of X.i=1,resultis assigned to the string"1". Wheni=2,resultis reassigned to"3", and so on. The loop returns the last value ofresult, which is"99". Perhaps you wantArray.new(50) { |i| "#{2*i+1}" } #=> ["1", "3",..., "99"].