Reduce the « 1 billion dollar mistake »!
Ruby 2.3 introduces the « safe navigation operator » &.
. It allows you to chain methods without risking an undefined method ‘xx’ for nil (NoMethodError). This code is almost equivalent to the version with &&
…
Almost equivalent, because the « safe navigation operator » short-circuits the method call only if the receiver is nil
. But false
is not nil
!
When you chain several methods, you’ll need to set it for each call if you start using it (in 99% of cases).
Link to Ruby documentation for the safe navigation operator.