Sha256: 84d96e536f6a6c8c592c1ffdcedac88e95e0bf0ff0cf24519042cc36533fc0c3

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

# = Iteration
#
#

class It
  attr_reader :index, :value, :prior, :after
  def initialize(array)
    @array = array
    @index = 0
    @value = array[0]
    @prior = []
    @after = array[1..-1]
  end
  def first? ; @index == 0 ; end
  def last?
    if Enumerable === self
      nil
    else
      @index == @array.length
    end
  end
  private
  def next_iteration
    @index += 1
    @prior << @value
    @value = @after.shift
  end
end

class Array
  # Iterate over each element of array using an iteration object.
  #
  #   [1,2,3].each_iteration do |it|
  #     p it.index
  #     p it.value
  #     p it.first?
  #     p it.last?
  #     p it.prior
  #     p it.after
  #   end
  #
  # on each successive iteration produces:
  #
  #   0          1          2
  #   1          2          3
  #   true       false      false
  #   false      false      true
  #   []         [1]        [1,2]
  #   [2,3]      [3]        []
  #
  # CREDIT: Trans

  def each_iteration
    if block_given?
      it = It.new(self)
      each do |e|
        yield(it)
        it.send(:next_iteration)
      end
    else
      return Enumerable::Enumerator.new(self, :each_iteration)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-2.6.0 lib/more/facets/iteration.rb
facets-2.5.0 lib/more/facets/iteration.rb
facets-2.5.1 lib/more/facets/iteration.rb
facets-2.5.2 lib/more/facets/iteration.rb