Ruby's tap method yields its receiver to a block, then returns that same receiver unchanged. This makes it useful for observing or modifying an object without interrupting a method chain. section 13.2.1
Inspect, then continue chaining
"Hello".tap { puts it.upcase }.reverse prints HELLO and evaluates to "olleH": the block observes the string, while reverse receives the original receiver. section 13.2.1
Use tap carefully when working with methods whose return values carry control-flow or chaining meaning:
- A bang method such as gsub! may return nil when no change occurs, but tap still returns the string. section 13.2.1
- Tap can clarify intent, but mixing in-place operations with methods returning copies can make longer chains harder to reason about. section 13.2.1
>> "Hello".tap { puts it.upcase }.reverse
HELLO
=> "olleH"
Runnable irb example showing tap's block result and unchanged receiver. section 13.2.1

