Ruby: tap method

The Tap Method : Yields self to the block, and then returns self.

According to the achieve of ruby code :

def tap
  yield self
  self
end
Used to debug
(1..10).to_a.select {|x| x.even? }.tap {|x| puts "evens: #{x}" }.map {|x| x*x }.tap {|x| puts "squares: #{x}" }
Reduce temporary variable and concise code
user = User.new
user.name = "John"
user

# tap way

User.new.tap { |user| user.name = "John" }
# superclass method
def self.new_with_session(params, session)
  new(params)
end

# subclass method without tap
def self.new_with_session(params, session)
  user = super
  if data = session["devise.facebook_data"]
    user.email = data["email"]
    user.confirmed_at = Time.now
  end
end

# subclass method use tap
def self.new_with_session(params, session)
  super.tap do |user|
    if data = session["devise.facebook_data"]
      user.email = data["email"]
      user.confirmed_at = Time.now
    end
  end
end

Reference: