Ruby 多行三元表达式?

标签 ruby ternary-operator

我正在尝试转换这样的东西:

if condition?
   expression1 line 1
   expression1 line 2
   expression1 line 3
else 
   expression2 line 1
end

对于三元,我的问题是:如何将多行放在一行的一个表达式中?你像java一样用分号分隔吗? 像这样?

condition? expression1 line 1; expression1 line 2; expression1 line 3 : expression2

最佳答案

In Ruby, it is always possible to replace newlines with semicolons, so you can, in fact, write your entire program in one single long giant line. Whether or not that is good for readability and maintainability, I will leave that up to you. (Note: you will sometimes have to insert parentheses for grouping in case of precedence mismatch.) Here is how you can write your conditional expression in a single line: if condition? then expression1 line 1; expression1 line 2; expression1 line 3 else expression2 line 1 end

关于Ruby 多行三元表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27969612/

相关文章:

ruby-on-rails - Rails - 如何获取 Assets 的文件路径(不是 url 路径)?

ruby-on-rails - 如何将 item_lookup 中的 IdType 从 ASIN 更改为 ISBN?

php - 是否可以在 PHP 中为此语句创建一个三元运算符?

javascript - 优先级 : Logical or vs. 三元运算符

java - 三元条件导致 Java 中奇怪的 CPU 使用率

`remove_const' 中的 Ruby Sinatra Hello World 错误:常量 URI::WFKV_ 未定义 (NameError)

ruby - RVM 错误 : Error running '__rvm_make -j2'

ios - 如何使用 if 逻辑或三元运算符在 Swift 中设置枚举案例的 rawValue?

ruby - 线程在 Ruby Shoes 中工作了吗?

java - Java三元运算符的自增