Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
26 lines (25 sloc) 671 Bytes
layout title jumbo
post
Safe Method Chaining
&.

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
You can’t perform that action at this time.