Use .tap To Perform Operations And To Return The Object

—

by

in

Ruby .tap method is used to yields the object to the block and will return the modified object. For example,

Don’ts

def build_account account = Account.new account.name = "example" account.email = "[email protected]" account end

Do’s

This method has a temporary variable to form and return the account object. We can change the above method just by using the .tap method.def build_account Account.new.tap do |user| account.name = "example" account.email = "[email protected]" end end

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *