Class: Fibber

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/bin/math_server.rb

Overview

Holds state for a fibonacci series

Instance Method Summary collapse

Constructor Details

#initialize(limit) ⇒ Fibber

Returns a new instance of Fibber.



50
51
52
53
# File 'src/ruby/bin/math_server.rb', line 50

def initialize(limit)
  fail "bad limit: got #{limit}, want limit > 0" if limit < 1
  @limit = limit
end

Instance Method Details

#generatorObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'src/ruby/bin/math_server.rb', line 55

def generator
  return enum_for(:generator) unless block_given?
  idx, current, previous = 0, 1, 1
  until idx == @limit
    if idx.zero? || idx == 1
      yield Math::Num.new(num: 1)
      idx += 1
      next
    end
    tmp = current
    current = previous + current
    previous = tmp
    yield Math::Num.new(num: current)
    idx += 1
  end
end