Ruby 三元运算符结构 [英] Ruby ternary operator structure

查看:46
本文介绍了Ruby 三元运算符结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

puts bool ? "true" : "false"

是正确的,但是

bool ? puts "true" : puts "false"

不是.有人可以向我解释这是为什么吗?

is not. Can somebody explain to me why this is?

附注:

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

效果也很好.

推荐答案

当您不将括号放在方法调用上时,Ruby 假定您希望行尾的所有内容都作为参数.也就是说,这些调用是等价的(并且是无效的):

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

这篇关于Ruby 三元运算符结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆