Safe Navigation Operator (&.) (Ruby)
The safe navigation operator &. calls a method only if the receiver is not nil; if it is nil, the expression returns nil instead of raising a NoMethodError. It avoids manual nil checks.
Ruby
Introduced in Ruby 2.3, user&.name returns user.name when user is non-nil and nil when user is nil — no crash. It replaces the verbose user && user.name pattern and chains: order&.customer&.email short-circuits to nil at the first nil link.
It targets exactly the NoMethodError: undefined method for nil:NilClass error that's so common. Combine it with || to supply a fallback: user&.name || "guest". Note it only guards against nil specifically, since nil is the only object you typically can't call your method on.
Use it to safely traverse possibly-missing data (parsed JSON, optional associations) without nested conditionals. It's Ruby's counterpart to Kotlin's ?. and JavaScript's ?. optional chaining.
Syntax
object&.method # nil if object is nilExamples
Ruby
name = nil
puts name&.upcase.inspect
puts "hi"&.upcaseOutput:
nil
HI