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
Leave a Reply