简体   繁体   English

Ruby三元运算符结构

[英]Ruby ternary operator structure

puts bool ? "true" : "false"

is proper, but 是适当的,但是

bool ? puts "true" : puts "false"

is not. 不是。 Can somebody explain to me why this is? 有人可以向我解释为什么吗?

Side note: 边注:

bool ? ( puts "true" ) : ( puts "false" )

works fine as well. 也可以正常工作。

When you don't put the parentheses on a method call, Ruby assumes you want everything to the end of the line to be the arguments. 当您不将括号放在方法调用上时,Ruby会假定您希望将行尾的所有内容都作为参数。 That is to say, these calls are equivalent (and invalid): 也就是说,这些调用是等效的(并且无效):

bool ? puts "true" : puts "false"
bool ? puts("true" : puts "false")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM