Recall & Review
beginner
What is the ternary operator in Ruby?
The ternary operator is a short way to write an if-else statement in one line. It uses the syntax: condition ? true_value : false_value.
Click to reveal answer
beginner
How does the ternary operator improve code readability?
It makes simple if-else decisions concise and easy to read by reducing multiple lines into one line.
Click to reveal answer
beginner
Example: What does this Ruby code return? <br>
age = 20<br>result = age >= 18 ? 'Adult' : 'Minor'It returns 'Adult' because the condition age >= 18 is true.
Click to reveal answer
intermediate
Can the ternary operator be nested in Ruby?
Yes, but nesting ternary operators can make code hard to read. It's better to use regular if-else for complex conditions.
Click to reveal answer
beginner
What is the difference between ternary operator and if-else in Ruby?
Ternary operator is a compact form for simple if-else statements. If-else can handle multiple lines and complex logic.
Click to reveal answer
What symbol separates the true and false parts in a Ruby ternary operator?
✗ Incorrect
The colon ':' separates the true and false expressions in the ternary operator.
Which of these is a correct Ruby ternary operator syntax?
✗ Incorrect
The correct syntax is condition ? true_value : false_value.
What will this Ruby code output? <br>
puts 5 > 10 ? 'Yes' : 'No'✗ Incorrect
Since 5 is not greater than 10, the condition is false, so it outputs 'No'.
Is it good practice to use ternary operators for complex multiple conditions?
✗ Incorrect
Using ternary operators for complex conditions makes code hard to read; use if-else instead.
What does the ternary operator return if the condition is true?
✗ Incorrect
If the condition is true, the ternary operator returns the expression after the '?'.
Explain how the ternary operator works in Ruby and give a simple example.
Think of it as a short if-else in one line.
You got /5 concepts.
When should you avoid using the ternary operator in Ruby?
Consider how easy it is to understand your code.
You got /3 concepts.