Safe Navigation Operator
The safe navigation operator (&.
) is used to make method calls or access attributes on a variable that may be nil
. Rather than triggering a NoMethodError
, the safe navigation operator returns nil
.
Not what you're looking for? The &
symbol is used in several syntactically distinct ways in Ruby. Head over to the Ampersand page to get a lay of the land.
book = nil
book.author.name # => NoMethodError
book&.author&.name # => nil