为什么`return a or b`在Ruby中是一个空值表达式错误? [英] Why is `return a or b` a void value expression error in Ruby?
问题描述
这很好:
def foo
a or b
end
这样也好:
def foo
return a || b
end
这会返回空值表达式
:
def foo
return a or b
end
为什么?它甚至没有被执行;它未能通过语法检查.空值表达式
是什么意思?
Why? It doesn't even get executed; it fails the syntax check. What does void value expression
mean?
推荐答案
return a or b
被解释为(return a) or b
,所以return a
是计算 (return a) 或 b
的值所必需的,但由于 return
永远不会留下值(因为它会转义从那个位置开始),它的设计目的不是在原始位置返回一个有效值.因此整个表达式留下了 (some_void_value) 或 b
,并被卡住了.就是这个意思.
return a or b
is interpreted as (return a) or b
, and so the value of return a
is necessary to calculate the value of (return a) or b
, but since return
never leaves a value in place (because it escapes from that position), it is not designed to return a valid value in the original position. And hence the whole expression is left with (some_void_value) or b
, and is stuck. That is what it means.
这篇关于为什么`return a or b`在Ruby中是一个空值表达式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!