Posted By

narkisr on 02/07/09


Tagged

ruby redo


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Ruby redo statment


 / 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/).

  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  

You need to login to post a comment.