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
nil it will stop calling methods and just return nil. This has been a similar feature in Rails for a long time using the
def street_name user.try(:address, :street) end