Safe Navigation Operator
With the release of Ruby 2.3, the safe navigation operator (&.) is now available. This addition to the Ruby language allows you to collapse all those pesky nil checks into the accessor call they are guarding. Consider this snippet of common Ruby code:
if user && user.authenticate(params[:password])
  # proceed with logged in user
endWith the safe navigation operator, the predicate can now be collapsed:
if user&.authenticate(params[:password])
  # proceed with logged in user
endIf user is nil, then the predicate will evaluate to false and the body of the if-statement will be passed over.
Last updated
Was this helpful?