ruby之三元运算符比if更为强大简洁。案例
puts 1 < 2 ? "One is less than two!" : "One is not less than two."
这是最容易出错的就是我们会这样写
1 < 2 ? puts "One is less than two!" : puts "One is not less than two."
这是错误的语法,ruby识别不了。因为可以这样理解,从根本上来说这只是一个表达式。不存在一个表达式有两个puts。
ruby之三元运算符比if更为强大简洁。案例
puts 1 < 2 ? "One is less than two!" : "One is not less than two."
这是最容易出错的就是我们会这样写
1 < 2 ? puts "One is less than two!" : puts "One is not less than two."
这是错误的语法,ruby识别不了。因为可以这样理解,从根本上来说这只是一个表达式。不存在一个表达式有两个puts。