Does Ruby have syntax for safe navigation operator of nil values, like in Groovy?

后端 未结 4 603
傲寒
傲寒 2020-12-17 09:19

In Groovy, there is a nice syntax for working with null values.

For example, I can do an if statement:

if (obj1?.obj2?.value) {

}
<
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 09:41

    Ruby 2.3 will have support for the safe navigation operator:

    obj1&.obj2&.value
    

    https://www.ruby-lang.org/en/news/2015/11/11/ruby-2-3-0-preview1-released/

提交回复
热议问题