Sha256: 03860f2bb3f7323fb04d1ea2e5544b3c8e1edd15762414b6609ab57405aa53e0

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

# open core class
class Enumerator
  # Iterates with whether the item is the last item.
  #
  #   [1,2].each.with_last { |item, is_last| puts [item, is_last] }
  #   # => [1, false] [2, true]
  #
  #   [1,2].map.with_last { |item, is_last| "#{item}#{is_last ? '.' : ', '}" }.join
  #   # => "1, 2."
  #
  #   %w[hoge fuga].map.with_index.with_last { |item, index, is_last| "#{index}: #{item}#{is_last ? '.' : ', '}" }.join
  #   # => "0:hoge, 1:fuga."
  def with_last
    return to_enum :with_last unless block_given?

    each do |*args|
      begin
        self.next
        peek
        yield(*args, false)
      rescue StopIteration => _e
        yield(*args, true)
      end
    end
  end
end

# open core class
module Enumerable
  # Iterates with whether the item is the last item.
  #
  #   [1,2].each_with_last { |item, is_last| puts [item, is_last] }
  #   # => [1, false] [2, true]
  def each_with_last
    return to_enum :each_with_last unless block_given?

    each.with_last { |*args, last| yield(*args, last) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
with_last-0.2.0 lib/with_last/core_ext.rb