class MTK::Patterns::Palindrome

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.

Public Instance Methods

repeat_ends?() click to toggle source

true if the first/last element are repeated when the ends are reached, else false

# File lib/mtk/patterns/palindrome.rb, line 9
def repeat_ends?
  @repeat_ends ||= @options.fetch :repeat_ends, false
end

Protected Instance Methods

advance() click to toggle source

(see Pattern#advance)

# 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
rewind_or_cycle(is_cycling=false) click to toggle source

(see Pattern#rewind_or_cycle)

Calls superclass method
# File lib/mtk/patterns/palindrome.rb, line 17
def rewind_or_cycle(is_cycling=false)
  @direction = 1
  super
end