为什么在Ruby中`将a或b返回一个无效值表达式错误? [英] Why is `return a or b` a void value expression error in Ruby?

查看:86
本文介绍了为什么在Ruby中`将a或b返回一个无效值表达式错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很好:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆