Class: FibonacciEnumerator

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Includes:
Enumerable
Defined in:
lib/fibonacci_enumerator.rb,
lib/fibonacci_enumerator/version.rb

Overview

rubocop:disable Style/StaticClass

Constant Summary collapse

VERSION =

Returns:

  • (String)
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFibonacciEnumerator

Returns a new instance of FibonacciEnumerator.



35
36
37
# File 'lib/fibonacci_enumerator.rb', line 35

def initialize
  reset_cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



33
34
35
# File 'lib/fibonacci_enumerator.rb', line 33

def cache
  @cache
end

Class Method Details

.[]Object

Parameters:

  • index (Integer, Range)


22
23
24
# File 'lib/fibonacci_enumerator.rb', line 22

def at(index)
  new.at(index)
end

.at(index) ⇒ Object

Parameters:

  • index (Integer, Range)


19
20
21
# File 'lib/fibonacci_enumerator.rb', line 19

def at(index)
  new.at(index)
end

.each {|| ... } ⇒ Enumerator, self

Yield Parameters:

  • (Integer)

Returns:

  • (Enumerator, self)


26
27
28
# File 'lib/fibonacci_enumerator.rb', line 26

def each(&block)
  new.each(&block)
end

Instance Method Details

#at(index) ⇒ Integer

Parameters:

  • index (Integer, Range)

Returns:

  • (Integer)


41
42
43
44
45
# File 'lib/fibonacci_enumerator.rb', line 41

def at(index)
  return at_int(index) if index.is_a?(::Integer)

  at_range(index)
end

#each {|| ... } ⇒ Enumerator, self

Yield Parameters:

  • (Integer)

Returns:

  • (Enumerator, self)


49
50
51
52
53
54
55
56
57
# File 'lib/fibonacci_enumerator.rb', line 49

def each
  return enum_for(:each) unless block_given?

  (0..).each do |i|
    yield at_int(i)
  end

  self
end

#reset_cachevoid

This method returns an undefined value.



60
61
62
# File 'lib/fibonacci_enumerator.rb', line 60

def reset_cache
  @cache = [0, 1, 1]
end