This is because nil&.nil? is shorthand for nil && nil.nil?. This would evaluate to nil && true, which is then nil.
(nil && x).nil? always evaluates to true, for any value of x.
While the syntax has power, this specific case has some potential to become a 'gotcha' for a developer:
(stuff&.things).nil? => This produces true if stuff doesn't exist, or stuff.things returns nil.
vs. the below case:
stuff&.things&.nil? => This produces nil in every case except the case where stuff.things returns something other than nil, in which case it would return false.
Because of the difficulty in normal boolean logic of differentiating between false and nil, it is unlikely that this would make sense in normal logic.
tryin Rails? - Jwan622try. - Franklin Yunil?on something that is obviouslynil(like the result ofnil&.) should logically return true, just likenil.nil?does - Magnenil?on the result ofnil&., but it's rather a part of the initial expressionnil && nil.nil?, which is ignored in this evaluation, since the first part of that expression isniland thus returns. See Oeste's answer. - Magne