Tales of code crunching from the FreeAgent Engineering team

Post navigation

Ruby 2.5: yield_self

yield_self is coming to Ruby 2.5. What is this long requested feature, and how does it work?

Some features take a while to get into a Ruby release. As you can see from the original request on Ruby’s Redmine issue tracker, yield_self has been brewing for 5 years. It has been waiting on a good name, and the Ruby team has settled on one.

But what is it? To understand it, perhaps it is best to look at some existing features.
Ruby blocks are one of the ways of passing around functions in Ruby. Every method parameter list has an implicit block argument, which can be used within the method. One of the ways of activating a block is to call yield. For example:

About James

James spends his time at FreeAgent reading, writing and occasionally deleting code. He finds joy in helping people, so making the lives of small businesses easier is right up his alley. You can find him frantically amplifying other voices over at @sarcainian.