Created
March 22, 2016 15:24
-
-
Save flori/bb990bdb25e482f7c8fa to your computer and use it in GitHub Desktop.
Fibonacci Generator in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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