是否有理由让if语句后跟另一个以ruby的'and'运算符(&&)结尾的行? [英] Is there a reason for an if statement followed by another to end the line with an 'and' operator (&&) in ruby?
问题描述
我正试图弄清楚为什么它起作用,以及这是否是一种不好的做法.
I'm trying to figure out why this works and whether it's a bad practice.
以下代码将引发 SyntaxError :
if true &&
puts "ok"
end
尽管以下代码不会,但将按预期执行:
Though the following code will not, it will execute as expected:
if true &&
if true
puts "ok"
end
end
我在编写带有 if 语句的红宝石脚本时遇到了这个问题,该语句包含很大的条件链,因此我将每个脚本放在单独的行中,然后注释掉最后一个并执行脚本.此刻,我意识到 if 语句以&& 运算符结尾.当我期望它抛出时,我感到惊讶的是,不是因为紧接着又出现了另一个 if 语句.
I came across this while writing a ruby script with an if statement containing a big chain of conditions, so I put each one in a separate line and then I commented out the last one and executed the script. At the moment, I realized that the if statement ended with the && operator. While I expected it to throw, I was amazed that it didn't because another if statement followed right after.
我的红宝石版本是ruby 2.2.6p396 (2016-11-15 revision 56800) [x86_64-darwin16]
.
推荐答案
我正试图弄清楚为什么它起作用,以及这是否是一种不好的做法.
I'm trying to figure out why this works and whether it's a bad practice.
后一个问题很难回答,因为您的代码是如此抽象.此外,不良做法"是基于观点的,因此是不合时宜的.
The latter question is hard to answer because your code is so abstract. Also, "bad practice" is opinion-based and thus off-topic.
以下代码将引发 SyntaxError :
if true &&
puts "ok"
end
这是一条红鲱鱼.请阅读错误消息:
This is a red herring. Please, read the error message:
SyntaxError: unexpected string literal, expecting `do' or '{' or '('
puts "ok"
^
它没有说明if
.
我希望您的代码等同于
if true && puts "ok"
end
确实引起了相同的SyntaxError
:
SyntaxError: unexpected string literal, expecting `do' or '{' or '('
if true && puts "ok"
^
我们可以进一步缩小到
true && puts "ok"
这是因为&&
的优先级高于空格分隔的参数列表,因此该行被解析为
Which is because &&
has higher precedence than a whitespace-delimited argument list, and thus this line is parsed as
(true && puts) "ok"
因此,要消除歧义,我们需要在参数列表中添加括号:
So, to disambiguate, we need to add parentheses to the argument list:
if true &&
puts("ok")
end
现在代码可以按预期工作了.
And now the code works as expected.
尽管以下代码不会,但将按预期执行:
Though the following code will not, it will execute as expected:
if true &&
if true
puts "ok"
end
end
的确,这是完全合法的.该行末尾的二进制运算符将隐式地继续该行,即此代码等效于:
Indeed, this is perfectly legal. A binary operator at the end of the line will implicitly continue the line, i.e. this code is equivalent to:
if true && if true
puts "ok"
end
end
或
if true && if true then puts "ok" end
end
或
if true && if true then puts "ok" end then end
我在写带有 if 语句的红宝石脚本时遇到了这个问题
I came across this while writing a ruby script with an if statement
这是您困惑的根源:这不是if
表达式. (从技术上讲,ISO Ruby语言规范将其称为条件表达式.)Ruby中没有语句,只有表达式.
And here is the source of your confusion: this is not an if
statement. This is an if
expression. (Technically, the ISO Ruby Language Specification calls it a conditional expression.) There are no statements in Ruby, only expressions.
我感到惊讶的是,那不是因为紧随其后的另一个 if 陈述.
不是声明.它是一个与其他任何表达式一样的表达式. foo && bar
是完全合法的,而foo && if true then 23 end
也是完全合法的,因为它是完全相同的东西:一个&&
运算符,其两侧都有一个表达式.
It isn't a statement. It is an expression like any other expression. foo && bar
is perfectly legal and foo && if true then 23 end
is also perfectly legal because it is the exact same thing: an &&
operator with an expression on either side.
这篇关于是否有理由让if语句后跟另一个以ruby的'and'运算符(&&)结尾的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!