为什么`return a or b`在Ruby中是一个空值表达式错误? [英] Why is `return a or b` a void value expression error in Ruby?

查看:12
本文介绍了为什么`return a or b`在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屋!

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