I can’t figure out how the n.times
line works in the ruby code given below, specifically what the commas mean and how the new
variable is updated.
def fib(n)
raise "fib not defined for negative numbers" if n < 0
new, old = 1, 0
n.times {new, old = new + old, new}
old
end
The code works, and was code given by user pjs in response to a question about fibonacci sequences in ruby, at this stackoverflow question: Ruby Fibonacci algorithm.
I don’t understand what’s going on inside the block.