Ruby redo statment


/ Published in: Ruby
Save to your folder(s)

An example of redo statement in Ruby, redo makes an iteration step repeat itself again (http://www.rubyrailways.com/rubys-most-underused-keyword/).


Copy this code and paste it in your HTML
  1. # no recursion (1.8 has no tail call optimization)
  2. def fib(i)
  3. acc = lambda do |i, n, result|
  4. if i == -1
  5. result
  6. else
  7. i, n, result = i - 1, n + result, n
  8. redo
  9. end
  10. end.call(i, 1, 0)
  11. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.