Ruby introduces new method Kernel#yield_self [Feature #6721] since the version 2.5.0.

Kernel#yield_self works similarly to the method Object#tap , but they return different values.

Object#tap yields self to the block and then returns self. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.

Example:

( 1 .. 10 ) . tap { | x | puts "original: #{ x } " } . to_a . tap { | x | puts "array: #{ x } " } . select { | x | x . even? } . tap { | x | puts "evens: #{ x } " } . map { | x | x * x } . tap { | x | puts "squares: #{ x } " } # => original: 1..10 # => array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # => evens: [2, 4, 6, 8, 10] # => squares: [4, 16, 36, 64, 100]

Kernel#yield_self yields self to the block and then returns the result of the block execution.

Example: