cwchen

Closure demo in Ruby

Feb 4th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.23 KB | None | 0 0
  1. def make_fibonacci
  2.   current = 0
  3.   _next = 1
  4.   lambda do
  5.     fibonacci = current
  6.     current, _next = _next, current + _next
  7.     return fibonacci
  8.   end
  9. end
  10.  
  11. iterator = make_fibonacci
  12.  
  13. 10.times do
  14.   num = iterator.call
  15.   puts num
  16. end
Add Comment
Please, Sign In to add comment