Tap Method in Ruby

There is a “tap” method in Ruby and I find it is very useful.

The definitionof “tap” in ruby documentaion is:

1
Yields self to the block, and then returns self. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.

Put it anonther way:

1
It’s a helper for call chaining. It passes its object into the given block and, after the block finishes, returns the object:
1
2
3
an_object.tap do |o|
  # do stuff with an_object, which is in o
end # ===> an_object

Another example:

1
2
3
4
5
6
7
8
9
10
11
def hash_table(numbers, target)
    [].tap do |result|
      hash = {}
      numbers.each_with_index do | n, i |
        if j = hash[target - n]
          return result << j + 1 << i + 1
        end
        hash[n] = i
      end
    end
  end