Return to Snippet

Revision: 11506
at February 7, 2009 16:00 by narkisr


Updated Code
# 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

Revision: 11505
at February 7, 2009 15:55 by narkisr


Initial Code
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

Initial URL

                                

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

Initial Title
Ruby redo statment

Initial Tags
ruby

Initial Language
Ruby