0033 Yield_self

For method chaining

The yield_self or then method, defined in Kernel, returns the result of the block called with self as argument.


The main advantage is the ability to chain methods.


Without a block, the method returns an Enumerator for filtering, transforming, …


You’ll have noticed that then is a reserved keyword, but Ruby does allow such a method to be defined, so it was intentionally chosen note#14, note#17.

Links to Ruby documentation for yield_self and then.