Tap Method

02 Aug 2018

As of Ruby 1.9 there is a .tap method on all objects in Ruby. Tap allows you to tap into an object, do something with that object, and automatically return that object. Here is an example.

# before
def new_manager
  user = User.new
  user.role = 'Manage'

# using tap
def new_manager
  # this returns the newly created user
  User.new.tap do |u|
    u.role = 'Manager'

A great use case for the .tap method is method chaining with methods that don’t return self. Say you have a method on a CreditCard object .pay_balance! that returns true or false if the payment goes through. You also want to get the .last4 on the card after that to show in the view. You could do something like this as always.

last4 = credit_card.last4

With .tap you could simply do:

last4 = credit_card.tap { |card| card.pay_balance! }.last4

# or more simply
last4 = credit_card.tap(&:pay_balance!).last4
Improvements, bugs, typos? Contribute on GitHub ❤️