DEV Community

Cover image for Ruby 2.5 - Tap and Yield Self

Ruby 2.5 - Tap and Yield Self

Ryan Palo on January 01, 2018

I was reading through the Ruby 2.5.0 release blurb, referred from Ben Halpern's article here, and I saw that one of the new features is the #yield_...
Collapse
 
jesus_castello profile image
Jesus Castello • Edited

I love yield_self & tap, thanks for the article!

Btw you can get a similar effect pre-2.5 with to_enum(:tap).

That converts tap into an Enumerator object, which allows you to chain it with other methods without having to pass a block to tap.

Collapse
 
rpalo profile image
Ryan Palo

Neat! Thanks for the tip!

Collapse
 
joellarsson profile image
Joel 🦇 Larsson

Great article! I've used this several times when I forgot about yield_self. Thanks very much for providing some simple use cases.

I also wanted to point out a typo in the line: .yield_self(&:shout), it should not contain the :. It should look like this to work: .yield_self(&shout). It got me confused why it didn't work for a while :)

Collapse
 
rpalo profile image
Ryan Palo

Thanks! I fixed it. I appreciate the heads up!