Sha256: a6ed2ddf3c7159a967111e3595718dc900abcc48b29a4ec763c9d60f17e3f861

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Loopable
# Mixin for Array like objects

  #set the position
  def set_position(index)
    @position = index
  end
  alias_method :set_pos, :set_position
  
  #returns the position ( current index)
  def position
    unless(defined?(@position)) then
      @position = 0
    end
    @position
  end
  alias_method :current_index, :position
  alias_method :pointer, :pointer
  
  #retruns the object at position
  def current
    #if self[position].respond_to?(:loopable_current) then
      self[position].loopable_current if self[position].respond_to?('loopable_current')
    #end
    self[position]
  end

  #increments position, looping back to begining
  #if necessary
  # returns the object at the new position
  def next
    if(position == self.size - 1) then
      @position = 0
    else
      @position = @position + 1
    end
    current
  end



  #moves the position forward a number of steps and
  #returns the object stored there
  def forward(steps = 1)
    steps.times { self.next }
    current
  end
  
  # decrements position, looping back from the end
  #if necessary
  def prev
    if(position == 0) then
      @position = self.size - 1
    else
      @position = @position - 1
    end
      current
  end
  
  #moves the position back a number of steps and
  #returns the object stored there
  def backward(steps = 1)
    steps.times { self.prev }
    current
  end

end # => module Loop

class Loop < Array
  include Loopable
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
conv-0.0.4 lib/loop.rb
conv-0.0.3 lib/loop.rb