Class: Humanoid::Collections::CyclicIterator
- Defined in:
- lib/humanoid/collections/cyclic_iterator.rb
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
Instance Method Summary collapse
-
#initialize(array) ⇒ CyclicIterator
constructor
Performs iteration over an array, if the array gets to the end then loop back to the first.
-
#next ⇒ Object
Get the next element in the array.
Constructor Details
#initialize(array) ⇒ CyclicIterator
Performs iteration over an array, if the array gets to the end then loop back to the first.
Example:
CyclicIterator.new([ first, second ])
14 15 16 |
# File 'lib/humanoid/collections/cyclic_iterator.rb', line 14 def initialize(array) @array, @counter = array, -1 end |
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
6 7 8 |
# File 'lib/humanoid/collections/cyclic_iterator.rb', line 6 def counter @counter end |
Instance Method Details
#next ⇒ Object
Get the next element in the array. If the element is the last in the array then return the first.
Example:
iterator.next
Returns:
The next element in the array.
28 29 30 31 |
# File 'lib/humanoid/collections/cyclic_iterator.rb', line 28 def next (@counter == @array.size - 1) ? @counter = 0 : @counter = @counter + 1 @array[@counter] end |