The safe navigation operator (&.
) provides a concise way to handle potentially nil objects. It returns nil instead of raising a NoMethodError when encountering nil.
# Without safe navigation
if user && user.profile && user.profile.address
puts user.profile.address
end
# With safe navigation
puts user&.profile&.address
Perfect for dealing with optional associations, API responses, and database records that might not exist. Use it to write more elegant and defensive code.