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.

0033-yield_self_1.png

The main advantage is the ability to chain methods.

0033-yield_self_2.png

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

0033-yield_self_3.png

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.