Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Enumerator::Lazy#tap_each

lazy.tap_each { |item| ... } → lazy_enumerator

Passes each element through to the block for side effects only, without modifying the element or affecting the enumeration. Returns a new lazy enumerator.

This is useful for debugging or logging inside lazy chains, without breaking laziness or misusing map.

Example
(1..).lazy
      .tap_each { |x| puts "got #{x}" }
      .select(&:even?)
      .first(3)
# prints: got 1, got 2, ..., got 6
# returns: [2, 4, 6]

Similar in intent to Java’s Stream#peek.