Sha256: c545364f9dd5586be9452a1c6b9898b5037c3da6edfa2be4415c2bb28ce36f31

Contents?: true

Size: 775 Bytes

Versions: 1

Compression:

Stored size: 775 Bytes

Contents

# frozen_string_literal: true

# Array immutable each method
class Array
  # Iterates the given block for each element with index.
  #
  # This implementation is considered immutable because it does not modify
  # the array's size or structure during iteration. The array's length is
  # calculated once at the beginning and remains constant throughout the iteration.
  #
  # 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
    length = self.length
    while i < length
      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/immutable/array/each.rb