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
enumerator.c
View on GitHub ↗
static VALUE
lazy_tap_each(VALUE obj)
{
if (!rb_block_given_p())
{
rb_raise(rb_eArgError, "tried to call lazy tap_each without a block");
}
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_tap_each_funcs);
}
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.