Sha256: 1505674e0cdbb77ada81bc21dbc8d4c0bd964fa6961bd6120305f9e5908694e7

Contents?: true

Size: 869 Bytes

Versions: 1

Compression:

Stored size: 869 Bytes

Contents

# frozen_string_literal: true

# Array
class Array
  # Iterates the given block for each element with index.
  #
  # This implementation is considered mutable because:
  # 1. It recalculates self.length on each iteration, allowing for potential
  #    changes to the array's size during iteration.
  # 2. If the array is modified during iteration (e.g., by the yielded block),
  #    the method will operate on the modified array.
  #
  # If no block is given, returns an Enumerator object.
  #
  # @yield [Object] Passes each element of the array to the block.
  # @return [Array] Returns self.
  # @return [Enumerator] If no block is given.
  def each
    return to_enum(:each) { self.length } unless block_given?

    i = 0
    while i < self.length  # Note: self.length is evaluated on each iteration
      yield self[i]
      i = i.succ
    end
    self
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
native_ruby-0.1.0 lib/native_ruby/iterators/mutable/array/each.rb