Safe Method Chaining

24 Feb 2018
&.

Ever do something like this?

def street_name
  if user && user.address
    user.address.street
  end
end

Well as of Ruby 2.3 you can use the new safe method chaining syntax to prevent you from calling an undefined method on nil. Just add a & after each object you are unsure of if it will be nil or not.

def street_name
  user&.address&.street
end

If either user or address is nil it will stop calling methods and just return nil. This has been a similar feature in Rails for a long time using the try method.

def street_name
  user.try(:address, :street)
end
Improvements, bugs, typos? Contribute on GitHub ❤️