为什么在Ruby中`将a或b返回一个无效值表达式错误? [英] 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
这将返回void value expression
:
def foo
return a or b
end
为什么?它甚至没有被执行;它无法通过语法检查. void value expression
是什么意思?
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) or b
的值是必需的,但由于(some_void_value) or 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.
这篇关于为什么在Ruby中`将a或b返回一个无效值表达式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!