Skip to content

Instantly share code, notes, and snippets.

@hoff2
Created October 6, 2012 14:48
Show Gist options
  • Save hoff2/3845107 to your computer and use it in GitHub Desktop.
Save hoff2/3845107 to your computer and use it in GitHub Desktop.
yay Enumerator
fibs = Enumerator.new do |y|
recent = [0,1]
loop do
y << recent.last
recent = (recent << recent[0..1].inject(:+)).pop(2)
end
end
fibs.first(20) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment