There are a number of features that were added to Ruby in versions 1.9 or later
that don’t seem to be in wide use for some reason. One that I have a fondness
for is the tap
method.
The tap
method was added to the Object
class in Ruby 1.9. All it does is
yield self
to a block and then return self
.
Here’s a simple example:
Doesn’t that seem useful? Not really? Okay. Well, according to the docs the method is meant to “tap into” a method chain, in the same way you’d tap into a rich vein of silver. Let’s see if we can find the silver here.
Imagine you need to create an object, call one of its methods, and then return it.
We’re getting closer.
What if you need to construct a string in a complicated way?
That’s so much better than the string = ""; string << "X" if something
alternative
that ends with string
at the end to make sure you return it. tap
takes care
of that for you.
The biggest benefit I’ve found of using tap
is that it makes it easy to create
a variable with limited scope. This can be very useful in Rails views, especially
if you use decorators to add view-related methods to an object.
Take this haml:
That’s all fine, but if you are in a situation where you want to use two different decorators in the same view, you might have something like this:
It can be hard to keep track of where a variable might be used and how. Also, those variable names are long to differentiate from themselves from each other. Try this instead:
I find that more readable and you have a good idea of the start and end of the scope of the variables.
I hope this encourages you to bring out Object#tap
for a little spin the next
chance you get.