Class: Laser::Cutter::Notching::InfiniteIterator

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ InfiniteIterator

Returns a new instance of InfiniteIterator.

Parameters:

  • array (Array) —

    the items, each answering clone



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.

Returns:

  • (Integer) —

    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.

Returns:

  • (Integer) —

    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.

Yield Parameters:

  • item (Object) —

    a copy of the next item

  • calls (Integer) —

    how many times next has been called, this time included

Yield Returns:

  • (Object) —

    what next returns instead

Returns:

  • (Object) —

    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