19

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
7
  • 5
    This is going to be very unclear to anyone else reading the code later on - is there a reason it has to be a ternary?
    – mcfinnigan
    Jan 15, 2015 at 17:43
  • 4
    Why do you think a ternary operator is better in this case? Jan 15, 2015 at 17:43
  • 1
    Seems like it's time to consider using another construct like if/else Jan 15, 2015 at 18:19
  • 4
    Taking long code expressions and shrinking them into short expressions is good. Taking long code expressions and shrinking them into a single line of long code is not a good idea. It makes for hard to read code. You may feel good doing it at first but you will only hate yourself later. Jan 15, 2015 at 18:46
  • 2
    Why do you want to do this? It won't run any faster, it doesn't really save space, and it'll greatly annoy anyone, including your future self, who has to maintain it. Just don't. Jan 15, 2015 at 22:01

4 Answers 4

32

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
0
26

You can express ternary over multiple lines this:

condition ?
  expression 1 :
  expression 2

And yes you'll need to use semicolons for multiple expressions (and brackets won't hurt).

Please don't do this, per rubocop style. Stick to single-line, or if blocks.

15

You should wrap the expressions in parenthesis:

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

You should keep in mind that this reduces readability of your code. You are probably better off using an if/else statement to improve readability. One resource I like to use when reviewing my ruby code is the community style guide. As it says in the introductory paragraph:

This Ruby style guide recommends best practices so that real-world Ruby programmers can write code that can be maintained by other real-world Ruby programmers.

Hope this helps

3
  • While it fits the objective of putting it all on one line, it breaks readability and maintainability, which are of greater importance. Saving space to sacrifice the others is a sure path to having a bad day in a code review. Jan 15, 2015 at 21:58
  • @theTinMan agreed. I would much rather prefer an if/else statement.
    – ptierno
    Jan 15, 2015 at 22:06
  • Say so in the answer. Giving an solution that works but is not the right way to do it isn't really a good answer because it propagates the assumption that it is good practice. Instead, it's OK to point the correct way. I think of it this way: Would I want someone on my team to be writing code like that, that I might have to maintain? Jan 15, 2015 at 22:55
7

The ternary operator requires a single block of instructions. It means you either group the instructions in using the parenthesis

condition = true
condition ? (puts("this"); puts("is"); puts("true")) : puts("this is false")

or in a begin/end block.

condition = true
condition ? begin puts("this"); puts("is"); puts("true") end : puts("this is false")

The fact that there is no simple, clean, way to achieve the result, should communicate to you that the ternary operator is not really designed for multiple statements. ;)

Don't try to use it in this case. Use a standard if/else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.