为什么 return 语句会破坏条件运算符?
在 ruby 中试验条件运算符,
def nada
false ? true : nil
end
def err
false ? true : raise('false')
end
按预期工作,但
def reflection
false ? true : return false
end
产生语法错误,意外的keyword_false,期望keyword_end
def reflection
false ? true : return(false)
end
并尝试使用括号语法错误,意外的tLPAREN,期望keyword_end
但
def reflection
false ? true : (return false)
end
有效正如预期的那样,更详细的 if
...then
...else
...end
def falsy
if false then true else return false end
end
也适用正如预期的那样。
那么条件(三元)运算符是怎么回事?
Experimenting with the conditional operator in ruby,
def nada
false ? true : nil
end
def err
false ? true : raise('false')
end
work as expected but
def reflection
false ? true : return false
end
produces a syntax error, unexpected keyword_false, expecting keyword_end
def reflection
false ? true : return(false)
end
and attempted with brackets syntax error, unexpected tLPAREN, expecting keyword_end
yet
def reflection
false ? true : (return false)
end
works as expected, and the more verbose if
...then
...else
...end
def falsy
if false then true else return false end
end
also works as expected.
So what's up with the conditional (ternary) operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样使用它,将整个
return
表达式放在括号中:当然,这样使用没有多大意义,但由于您正在试验(很好!),所以上面的方法有效!我认为该错误是由于 Ruby 语法的工作方式造成的 - 它需要某种结构来形成有效的表达式。
更新
引用草案规范中的一些信息:
注意。在上面,jump-expression包括
return
等等。You can use it like this, by putting the entire
return
expression in parentheses:Of course, it does not make much sense used like this, but since you're experimenting (good!), the above works! The error is because of the way the Ruby grammar works I suppose - it expects a certain structure to form a valid expression.
UPDATE
Quoting some information from a draft specification:
NB. In the above, jump-expression includes
return
among others.我认为这都与 ruby 解析器有关。
return
解析为三元运算符的 else 表达式false
而不是end
return false 时,它会感到惊讶括号中的
会导致 ruby 将整个内容解释为 else 表达式return(false)
不起作用,因为 ruby 仍在尝试仅解释return
部分作为 else 表达式,并且感到惊讶当它找到左括号时(更新)注意:我认为这不是一个很好的答案。
例如,一个很好的答案可以参考 ruby 语法来解释解析错误。
I think this is all related to the ruby parser.
return
as the else-expression of the ternary operatorfalse
instead ofend
return false
in parentheses causes ruby to interpret the entire thing as the else-expressionreturn(false)
doesn't work because ruby is still trying to interpret just thereturn
part as the else-expression, and is surprised when it finds a left-parenthesis (updated)Note: I don't think this is a great answer.
A great answer could, for example, explain the parse errors with reference to the ruby grammar.
ternery 运算符就是一个运算符。你不会从中回来。您从函数中返回。当您在 if 中放入 return 时,您将从 if 所在的函数返回。由于没有变量等待 if 结果的赋值,因此没有问题。当您从 ternery 运算符返回时,没有为变量分配任何值。
The ternery operator is just that, an operator. You don't return from it. You return from functions. When you put a return in an if, you return from the function that the if is in. Since there is no variable awaiting assignment from the result of the if, there is no problem. When you return from the ternery operator, there is no value assigned to the variable.