Tapping method chains with Ruby 1.9

| by Werner Schuster Follow 6 Followers on Feb 04, 2008. Estimated reading time: 2 minutes |
The idea for the tap method has been around for some time - but it has now been added to the standard Ruby library in Ruby 1.9. MenTaLguY, who blogged about the idea behind tap shows the simple code:
class Object
 def tap
 yield self

In Ruby 1.9, the tap method is defined in Object, making it available for every object in Ruby by default. The method takes a Block as argument, which it calls with self as argument - then the object is returned.

The indirection through the tap method seems like a complicated way of doing something with an object. The real benefit of this becomes clear when the object of interest is passed from one method to another without ever being assigned to a variable. This is common whenever methods are chained, particularly if the chain is long.
An example: without tap, a temporary variable is needed:
xs = blah.sort.grep( /foo/ )
p xs
# do whatever we had been doing with the original expression { |x| x.blah }

With tap:
blah.sort.grep( /foo/ ).tap { |xs| p xs }.map { |x| x.blah } 

This shows where tap is useful: without it, it's necessary to assign the object of interest to a local variable to use it - with tap it's possible to insert the Block that inspects the object right where the handover between the chained methods happens. This gets particularly useful with APIs that expose so called Fluent Interfaces - i.e. APIs that encourage method chaining. Here a Java example from Martin Fowler's website:
 .with(6, "TAL")
 .with(5, "HPK").skippable()
 .with(3, "LGV")

In case this code has a bug, tap allows to look at the object at an arbitrary stage (i.e. between every call) by simply inserting a tap block. This is also useful with debugging tools, which often don't support looking at anonymous return values of methods.

It's important to mention that tap is normally about causing some kind of side effect without changing the object (the Block's return value is ignored). However, it is of course possible to modify the object as long as it's mutable.

Users of Rails' ActiveSupport are already familiar with a similar method in the form of the returning method .

Of course, the tap method is not restricted to Ruby 1.9 - Ruby's Open Classes allow to do this on non-1.9 Ruby versions too.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread


Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you