Skip to content

Instantly share code, notes, and snippets.

@flori
Created March 22, 2016 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flori/bb990bdb25e482f7c8fa to your computer and use it in GitHub Desktop.
Save flori/bb990bdb25e482f7c8fa to your computer and use it in GitHub Desktop.
Fibonacci Generator in Ruby
$fib = Enumerator.new do |e|
a, b = 0, 1
loop do
e.yield a
b, a = a + b, b
end
end
def $fib.[](n)
each_with_index.find { |x, i| i == n }.first
end
p $fib.take(10), $fib[100]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment