Ruby ternary operator structure

安稳与你 提交于 2020-01-21 05:40:29

问题


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.


回答1:


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")


来源:https://stackoverflow.com/questions/16629567/ruby-ternary-operator-structure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!