Ruby multiline ternary expression

rubyternary-operator

I'm trying to convert something like this:

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

to a ternary, my question is: how do you put multiple lines into one expression on a single line? Do you separate by a semicolon like in java?
Like this?

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

Best Solution

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