Class: MTK::Patterns::Palindrome
- Defined in:
- lib/mtk/patterns/palindrome.rb
Overview
An endless enumerator that outputs an element one at a time from a list of elements, looping back to the beginning when elements run out.
Instance Attribute Summary
Attributes inherited from Pattern
#cycle_count, #element_count, #elements, #max_cycles, #max_elements, #min_elements, #options
Instance Method Summary collapse
-
#advance ⇒ Object
protected
Update internal state (index, etc) and set @current to the next element.
-
#repeat_ends? ⇒ Boolean
true if the first/last element are repeated when the ends are reached, else false.
-
#rewind_or_cycle(is_cycling = false) ⇒ Object
protected
Reset the pattern to the beginning.
Methods inherited from Cycle
Methods inherited from Pattern
#empty?, from_a, #initialize, #max_cycles_exceeded?, #max_elements_exceeded?, #min_elements_unmet?, #next, #rewind
Methods included from Groups::Collection
#==, #[], #clone, #concat, #each, #empty?, #enumerable_map, #first, #last, #map, #partition, #permute, #repeat, #reverse, #rotate, #size, #to_a
Constructor Details
This class inherits a constructor from MTK::Patterns::Cycle
Instance Method Details
#advance ⇒ Object (protected)
Update internal state (index, etc) and set @current to the next element.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mtk/patterns/palindrome.rb', line 23 def advance raise StopIteration if @elements.nil? or @elements.empty? # prevent infinite loops @index += @direction if @index >= @elements.length @direction = -1 @index = @elements.length - 1 @index -= 1 unless repeat_ends? or @elements.length == 1 elsif @index < 0 @direction = 1 @index = 0 @index += 1 unless repeat_ends? or @elements.length == 1 end @current = @elements[@index] end |
#repeat_ends? ⇒ Boolean
true if the first/last element are repeated when the ends are reached, else false
9 10 11 |
# File 'lib/mtk/patterns/palindrome.rb', line 9 def repeat_ends? @repeat_ends ||= @options.fetch :repeat_ends, false end |
#rewind_or_cycle(is_cycling = false) ⇒ Object (protected)
Reset the pattern to the beginning
17 18 19 20 |
# File 'lib/mtk/patterns/palindrome.rb', line 17 def rewind_or_cycle(is_cycling=false) @direction = 1 super end |