加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 用于安全导航操作符的语法糖(&.)

发布时间:2020-12-17 03:56:51 所属栏目:百科 来源:网络整理
导读:str = "Hello World!"str.[] /Hello/ # = "Hello"str[/Hello/] # = "Hello",syntactic suger versionstr = nilstr.[] /Hello/ # = nilstr.[/Hello/] # = SyntaxError: unexpected '[',expecting '('str[/Hello/] # = NoMethodError: undefined method `[]' f
str = "Hello World!"
str.[] /Hello/    # => "Hello"
str[/Hello/]      # => "Hello",syntactic suger version

str = nil
str&.[] /Hello/   # => nil
str&.[/Hello/]    # => SyntaxError: unexpected '[',expecting '('
str[/Hello/]      # => NoMethodError: undefined method `[]' for nil:NilClass

如何在[]方法的语法糖上使用安全导航操作符(&amp ;.)?

解决方法

How can the Safe Navigation operator(&.) be used on syntactic sugar for []method?

它不能.马茨不想要它.

此功能已被请求和拒绝两次:

> Bug #11618: Safe call syntax with aref or aset is
> Feature #11813: Extend safe navigation operator for [] and []= with syntax sugar

Matz says:

I am not going to add safe navigation for aref and aset.
Maybe you can use a.?[](x) (method call of an operator).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读