← Back to Ruby Operators

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.

book = nil
book.author.name # => NoMethodError
book&.author&.name # => nil

References