Yusuf Birader

The Ruby Tap Method

I’m always on the lookout for better ways to debug my code.

And the other day, I came across a little-know Ruby method that does just that.

The Ruby tap method is a method which takes the calling object i.e. self and passes it to a block argument. Strangely, tap then returns the original, unmodified object i.e. it returns self.

So why would a method which passes the calling object to a block, and then returns that same calling object, completely ignoring what happens in the block, be remotely useful?

Well, it turns out that tap is perfect for debugging method chains.

Debugging Method Chains Let’s consider an example method chain:

(1..10).to_a.select { |x| x.even? }.map { |x| x*x }

The above code creates an array of integers from 1 to 10, selects all even integers, and maps each even integer to its square.

But say that there is a problem with one of the intermediate methods in this chain. For example, if either to_a or select returned a nil value, it would cause the entire method chain to break down.

The tap method allows us to tap into a given chain to see the output.

For example, if we wanted to see what the select method was returning in the above code, we could do this:

(1..10).to_a.select { |x| x.even? }.tap { |x| p x }.map { |x| x * x }

In this case, the tap method would print the return value of select and also return this value, leaving the rest of the method chain unaffected. If for example, the select method was outputting nil, we could see this using tap and fix the error to ensure the full method chain is bug-free.

Even better, the tap method is available on all instances of the Object class. Since the Objectclass is the default root class of all Ruby objects, it is available on all Ruby objects by default, providing it is not overridden.

Final Thoughts

The Ruby tap method is a great method to add to your debugging arsenal. Its primary use case is to ‘tap into’ method chains, providing an easy way to check and eliminate erroneous return values that can cause a method chain to break down.


Subscribe

If you'd like to be kept in the loop for future writing, you can join my private email list. Or even just follow me on X/Twitter @yusufbirader.