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:
With the safe navigation operator, the predicate can now be collapsed:
If user
is nil
, then the predicate will evaluate to false
and the body of the if-statement will be passed over.
Last updated