How to #Tap That

Anna Rasshivkina
Mar 3, 2016 · 3 min read

Part of my series of easy primers on methods to eliminate code smell: needlessly lengthy or complex code.

tap

Code smell: You’re executing some action on a variable, and then have to return the variable in a separate step.

Have you ever had to perform some function on a variable, but that function doesn’t return the original variable — so, in a separate step, you then have return the original variable? For example:

Image for post
Image for post

Doesn’t it seem kind of weird that, in the strivingly elegant and concise Ruby, there wouldn’t be a method for this sort of thing? Luckily, with Ruby 1.9 came the #tap method.

You call this method on the object you ultimately want to return, and give it a block of code that you want to execute on your object. It executes the code and returns your updated object.

Why is it called tap? Because the method enables you to tap into a method chain and execute another command. In the Ruby doc, #tap’s only example usage is to inspect a variable in the midst of an operation. For example:

(1..10).select {|x| x%2==0}.tap {|x| puts "evens: #{x.inspect}"}

would print out “evens: [2, 4, 6, 8, 10]” and return the array.

But, aside from the fact that this is exactly what debuggers are for, this seems like a very uncreative application for a really useful method.

Instead, let’s look at the same array example from above, using #tap:

1) arr is a variable representing our array 2) our block contains the method we want to execute on our array 3) #tap automatically returns our modified array

We call this method on the array that we want to work with, and pass it a block containing the code we want to execute on it. The only variable in our block is just an epithet for this array. It may seem strange to have a variable in a method for the very object calling the method…but just go with it.

#Tap, though it looks like an iterator, is not. Instead, it’s almost like a parenthetical, a qualifier. Like saying, “Can you give me this array? (But take off that last element first, please.)” #Tap does just that.

Now let’s look at another, more practical example.

If you’re building your own classes in Ruby, you often find yourself creating an instance of a class, setting a variable for it, and then returning the instance object you just created. Without #tap, it looks like this:

Image for post
Image for post

Doesn’t that look redundant? A little ‘smelly,’ even? #Tap is a perfect candidate for this task. Using #tap, we can turn these three lines into one — which, incidentally, is a better representation of what we’re doing: a single thing, i.e. creating a new Dog instance. Let’s deodorize that code smell:

 1) our new Dog object  2) a variable representing our new  dog  object 3) the code we want to run on that object 4) #tap returns our complete new object
 1) our new Dog object  2) a variable representing our new  dog  object 3) the code we want to run on that object 4) #tap returns our complete new object
1) our new Dog object 2) a variable representing our new dog object 3) the code we want to run on that object 4) #tap returns our complete new object

We create our new Dog object and call #tap on it in one step. Within our block, we ask #tap to set the dog’s owner as “Jacki” before it returns the object to us. By the time we’re handed our new Dog instance, it’s filled out with all the information. Done and done!

Now, before I wrap up, a caveat on #tap: from my extensive research on the subject (i.e. browsing StackExchange forums), I’ve seen that this relatively young method is somewhat contentious. For those who aren’t familiar with it, it can be hard to understand at a glance what it does; “tap” is not a very illustrative method name. And yet, it is so handy in such a common situation. So, shorter code or friendlier code for unaccustomed eyes? Personally, I’d #tap that.

Synaptic Sugar

Code blog covering Ruby, Javascript, HTML, CSS, and other…

Anna Rasshivkina

Written by

All things language. Writer, translator, coder. www.thesewords.live

Synaptic Sugar

Code blog covering Ruby, Javascript, HTML, CSS, and other assorted coding topics. Attempting to make simple the confusing, with minimum nerd jargon. Written by Anna Rasshivkina.

Anna Rasshivkina

Written by

All things language. Writer, translator, coder. www.thesewords.live

Synaptic Sugar

Code blog covering Ruby, Javascript, HTML, CSS, and other assorted coding topics. Attempting to make simple the confusing, with minimum nerd jargon. Written by Anna Rasshivkina.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store