下面的代码在Ruby中是什么意思?
||=
它的语法有什么意义或原因吗?
下面的代码在Ruby中是什么意思?
||=
它的语法有什么意义或原因吗?
当前回答
A ||= b和A = b是一样的如果A。nil?或者a = b,除非a
但是所有3个选项都能显示相同的性能吗?在Ruby 2.5.1中
1000000.times do
a ||= 1
a ||= 1
a ||= 1
a ||= 1
a ||= 1
a ||= 1
a ||= 1
a ||= 1
a ||= 1
a ||= 1
end
在我的电脑上花费0.099秒,而
1000000.times do
a = 1 unless a
a = 1 unless a
a = 1 unless a
a = 1 unless a
a = 1 unless a
a = 1 unless a
a = 1 unless a
a = 1 unless a
a = 1 unless a
a = 1 unless a
end
用时0.062秒。这几乎快了40%。
然后我们还有:
1000000.times do
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
a = 1 if a.nil?
end
这需要0.166秒。
并不是说这将在一般情况下产生重大的性能影响,但是如果您确实需要最后一点优化,那么可以考虑这个结果。 顺便说一下:a = 1,除非a对新手来说更容易理解,否则它是不言自明的。
注1:重复分配行多次的原因是为了减少循环在测量时间上的开销。
注2:如果我在每次赋值前执行a=nil,结果是类似的。
其他回答
还请记住||=不是原子操作,因此,它不是线程安全的。根据经验,不要将它用于类方法。
||=称为条件赋值运算符。
它基本上像=一样工作,但如果一个变量已经被赋值,它将什么都不做。
第一个例子:
x ||= 10
第二个例子:
x = 20
x ||= 10
在第一个例子中,x现在等于10。然而,在第二个例子中,x已经被定义为20。所以条件运算符没有作用。执行X ||= 10后,X仍然是20。
一个常见的误解是,||= b并不等同于a = a || b,但它的行为就像|| a = b。
但这里有一个棘手的情况。如果a未定义,则|| a = 42将引发NameError,而||= 42将返回42。所以,它们似乎不是等价的表达式。
这个问题在Ruby邮件列表和Ruby博客上讨论得如此频繁,以至于现在Ruby邮件列表上甚至有一些线程,它们的唯一目的就是收集到Ruby邮件列表上讨论这个问题的所有其他线程的链接。
这里有一个:||= (OR Equal)线程和页面的最终列表
如果你真的想知道发生了什么,可以看看Ruby语言规范草案的第11.4.2.3节“缩写赋值”。
作为第一近似,
a ||= b
等于
a || a = b
并不等同于
a = a || b
然而,这只是一种近似,特别是在a未定义的情况下。根据是简单的变量赋值、方法赋值还是索引赋值,语义也有所不同:
a ||= b
a.c ||= b
a[c] ||= b
都有不同的待遇。
准确地说,||= b意味着“如果a是未定义的或假的(false或nil),将a设置为b并求值为(即返回)b,否则求值为a”。
其他人经常试图用||= b等价于|| a = b或a = a || b来说明这一点。这些等价有助于理解这个概念,但要注意,它们并非在所有条件下都是准确的。请允许我解释:
a ||= b ⇔ a || a = b? The behavior of these statements differs when a is an undefined local variable. In that case, a ||= b will set a to b (and evaluate to b), whereas a || a = b will raise NameError: undefined local variable or method 'a' for main:Object. a ||= b ⇔ a = a || b? The equivalency of these statements are often assumed, since a similar equivalence is true for other abbreviated assignment operators (i.e. +=,-=,*=,/=,%=,**=,&=,|=,^=,<<=, and >>=). However, for ||= the behavior of these statements may differ when a= is a method on an object and a is truthy. In that case, a ||= b will do nothing (other than evaluate to a), whereas a = a || b will call a=(a) on a's receiver. As others have pointed out, this can make a difference when calling a=a has side effects, such as adding keys to a hash. a ||= b ⇔ a = b unless a?? The behavior of these statements differs only in what they evaluate to when a is truthy. In that case, a = b unless a will evaluate to nil (though a will still not be set, as expected), whereas a ||= b will evaluate to a. a ||= b ⇔ defined?(a) ? (a || a = b) : (a = b)???? Still no. These statements can differ when a method_missing method exists which returns a truthy value for a. In this case, a ||= b will evaluate to whatever method_missing returns, and not attempt to set a, whereas defined?(a) ? (a || a = b) : (a = b) will set a to b and evaluate to b.
好的,好的,那么||= b等于什么?有办法在Ruby中表达这一点吗?
好吧,假设我没有忽略任何东西,我相信||= b在功能上等价于……(击鼓声)
begin
a = nil if false
a || a = b
end
坚持住!这不是第一个前面有noop的例子吗?嗯,不完全是。还记得我之前说的||= b只有当a是一个未定义的局部变量时,||= b才不等于|| a = b吗?如果为false,则a = nil确保a永远没有定义,即使这一行永远不会执行。Ruby中的局部变量是词法范围的。