Safe navigation operator (&.) for nil

sbs picture sbs · Jan 4, 2016 · Viewed 8.5k times · Source

As Ruby 2.3 introduces the Safe navigation operator(&.), a.k.a lonely operator, the behavior on nil object seems odd.

nil.nil?    # => true
nil&.nil?   # => nil

Is that designed to behave like this way? Or some edge case that slipped away when adding the lonely operator?

Answer

mwp picture mwp · Jan 4, 2016

foo&.bar is shorthand for foo && foo.bar, so what would you expect the result of the expression nil && nil.nil? to be?