Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Each instance of FibSequence remembers how many Fibonacci numbers are
- # desired using the @limit instance variable.
- # FibSequence#each is just an instance method that yields the
- # first @limit Fibonacci numbers one at a time.
- class FibSequence
- include Enumerable
- def initialize(limit) ; @limit = limit ; end
- def each
- case @limit
- when 0 then return
- when 1 then yield 1
- else
- yield 1
- yield 1
- lastfib,nextfib = 1,2
- @limit.downto(3) do
- yield nextfib
- lastfib, nextfib = nextfib, nextfib+lastfib
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement