我正在尝试转换这样的东西:
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/