Skip to content

Instantly share code, notes, and snippets.

@mpalmer
Last active August 29, 2015 14:05
Show Gist options
  • Save mpalmer/fcafcba4f489ac14cd58 to your computer and use it in GitHub Desktop.
Save mpalmer/fcafcba4f489ac14cd58 to your computer and use it in GitHub Desktop.
class Fibonnaci
include Enumerable
def initialize
@a = @b = 1
end
def each
loop do
yield @a
@a, @b = @b, @a + @b
end
end
end
class Fibonnaci
include Enumerable
def initialize(limit)
@a = @b = 1
@limit = limit
end
def each
while @a < @limit
yield @a
@a, @b = @b, @a + @b
end
end
end
def fibonnaci_enumerator(limit)
Enumerator.new do |y|
a = b = 1
while a < limit
y << a
a, b = b, a + b
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment