Safe Navigation on nil gotcha
tldr; Beware, both ActiveSupport
's try
and Ruby's safe navigation operator &.
will always return nil
when operating on a nil
.
For example, I would have expected either to return a response like so:
> nil.try(:to_s) => ""
> nil&.to_s => ""
However this is not the case:
> nil.try(:to_s) => nil
> nil&.to_s => nil
Tweet