安全导航操作符 (&.) for nil [英] Safe navigation operator (&.) for nil
本文介绍了安全导航操作符 (&.) for nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于 Ruby 2.3 引入了安全导航操作符 (&.
),也就是一个孤独的操作符,nil
对象上的行为似乎很奇怪.
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?
推荐答案
foo&.bar
是 foo && 的简写foo.bar
,那么你期望表达式 nil && 的结果是什么?nil.nil?
是?
foo&.bar
is shorthand for foo && foo.bar
, so what would you expect the result of the expression nil && nil.nil?
to be?
这篇关于安全导航操作符 (&.) for nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文