How is the conditional operator (? :
) used in Ruby?
For example, is this correct?
<% question = question.size > 20 ? question.question.slice(0, 20)+\"...\" : question.question %>
How is the conditional operator (? :
) used in Ruby?
For example, is this correct?
<% question = question.size > 20 ? question.question.slice(0, 20)+\"...\" : question.question %>
It is the ternary operator, and it works like in C (the parenthesis are not required). It\'s an expression that works like:
if_this_is_a_true_value ? then_the_result_is_this : else_it_is_this
However, in Ruby, if
is also an expression so: if a then b else c end
=== a ? b : c
, except for precedence issues. Both are expressions.
Examples:
puts (if 1 then 2 else 3 end) # => 2
puts 1 ? 2 : 3 # => 2
x = if 1 then 2 else 3 end
puts x # => 2
Note that in the first case parenthesis are required (otherwise Ruby is confused because it thinks it is puts if 1
with some extra junk after it), but they are not required in the last case as said issue does not arise.
You can use the \"long-if\" form for readability on multiple lines:
question = if question.size > 20 then
question.slice(0, 20) + \"...\"
else
question
end
puts true ? \"true\" : \"false\"
=> \"true\"
puts false ? \"true\" : \"false\"
=> \"false\"
Your use of ERB suggests that you are in Rails. If so, then consider truncate
, a built-in helper which will do the job for you:
<% question = truncate(question, :length=>30) %>
@pst gave a great answer, but I\'d like to mention that in Ruby the ternary operator is written on one line to be syntactically correct, unlike Perl and C where we can write it on multiple lines:
(true) ? 1 : 0
Normally Ruby will raise an error if you attempt to split it across multiple lines, but you can use the \\
line-continuation symbol at the end of a line and Ruby will be happy:
(true) \\
? 1 \\
: 0
This is a simple example, but it can be very useful when dealing with longer lines as it keeps the code nicely laid out.
It\'s also possible to use the ternary without the line-continuation characters by putting the operators last on the line, but I don\'t like or recommend it:
(true) ?
1 :
0
I think that leads to really hard to read code as the conditional test and/or results get longer.
I\'ve read comments saying not to use the ternary operator because it\'s confusing, but that is a bad reason to not use something. By the same logic we shouldn\'t use regular expressions, range operators (\'..
\' and the seemingly unknown \"flip-flop\" variation). They\'re powerful when used correctly, so we should learn to use them correctly.
Why have you put brackets around
true
?
Consider the OP\'s example:
<% question = question.size > 20 ? question.question.slice(0, 20)+\"...\" : question.question %>
Wrapping the conditional test helps make it more readable because it visually separates the test:
<% question = (question.size > 20) ? question.question.slice(0, 20)+\"...\" : question.question %>
Of course, the whole example could be made a lot more readable by using some judicious additions of whitespace. This is untested but you\'ll get the idea:
<% question = (question.size > 20) ? question.question.slice(0, 20) + \"...\" \\
: question.question
%>
Or, more written more idiomatically:
<% question = if (question.size > 20)
question.question.slice(0, 20) + \"...\"
else
question.question
end
%>
It\'d be easy to argument that readability suffers badly from question.question
too.
A simple example where the operator checks if player\'s id is 1 and sets enemy id depending on the result
player_id=1
....
player_id==1? enemy_id=2 : enemy_id=1
# => enemy=2
And I found a post about to the topic which seems pretty helpful.
One small variation of this is to use the convert to boolean !! operator like this
result = !!(condition == true)
The code condition ? statement_A : statement_B
is equivalent to
if condition == true
statement_A
else
statement_B
end