/ Published in: Ruby
An example of redo statement in Ruby, redo makes an iteration step repeat itself again (http://www.rubyrailways.com/rubys-most-underused-keyword/).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# no recursion (1.8 has no tail call optimization) def fib(i) acc = lambda do |i, n, result| if i == -1 result else i, n, result = i - 1, n + result, n redo end end.call(i, 1, 0) end