Ruby ternary operator structure

2020-03-04 09:48发布

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.

标签: ruby ternary
1条回答
叼着烟拽天下
2楼-- · 2020-03-04 10:40

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. That is to say, these calls are equivalent (and invalid):

bool ? puts "true" : puts "false"
bool ? puts("true" : puts "false")
查看更多
登录 后发表回答