Class: Laser::Cutter::Notching::InfiniteIterator
- Inherits:
-
Struct
- Object
- Struct
- Laser::Cutter::Notching::InfiniteIterator
- Defined in:
- lib/laser/cutter/notching/infinite_iterator.rb
Overview
Hands out copies of the items of an array in turn, starting again at the first after the last, for ever.
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#calls ⇒ Integer
How many times #next has been called.
-
#next_index ⇒ Integer
The index of the item #next returns.
Instance Method Summary collapse
-
#initialize(array) ⇒ InfiniteIterator
constructor
A new instance of InfiniteIterator.
-
#next {|item, calls| ... } ⇒ Object
A copy of the next item, or what the block returned.
Constructor Details
#initialize(array) ⇒ InfiniteIterator
Returns a new instance of InfiniteIterator.
16 17 18 19 20 |
# File 'lib/laser/cutter/notching/infinite_iterator.rb', line 16 def initialize(array) self.array = array self.calls = 0 self.next_index = 0 end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
8 9 10 |
# File 'lib/laser/cutter/notching/infinite_iterator.rb', line 8 def array @array end |
#calls ⇒ Integer
Returns how many times #next has been called.
13 |
# File 'lib/laser/cutter/notching/infinite_iterator.rb', line 13 attr_accessor :array, :next_index, :calls |
#next_index ⇒ Integer
Returns the index of the item #next returns.
13 |
# File 'lib/laser/cutter/notching/infinite_iterator.rb', line 13 attr_accessor :array, :next_index, :calls |
Instance Method Details
#next {|item, calls| ... } ⇒ Object
Returns a copy of the next item, or what the block returned.
26 27 28 29 30 31 32 33 |
# File 'lib/laser/cutter/notching/infinite_iterator.rb', line 26 def next item = array[next_index].clone self.next_index += 1 self.next_index %= array.size self.calls += 1 item = yield item, calls if block_given? item end |