Bird
Raised Fist0
Rubyprogramming~10 mins

Ternary operator in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Ternary operator
Evaluate condition
Yes↓
Return value if true
↓
End
No↓
Return value if false
↓
End
The ternary operator checks a condition and returns one value if true, another if false, all in one line.
Execution Sample
Ruby
age = 20
status = age >= 18 ? "Adult" : "Minor"
puts status
This code checks if age is 18 or more, then sets status to 'Adult' or 'Minor' accordingly, and prints it.
Execution Table
StepCondition (age >= 18)ResultValue assigned to statusOutput
120 >= 18true"Adult"
2N/AN/AN/AAdult
💡 Condition evaluated once; since true, 'Adult' assigned and printed; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2 (final)
ageundefined2020
statusundefined"Adult""Adult"
Key Moments - 2 Insights
Why does the ternary operator use a question mark and colon?
The question mark separates the condition from the true value, and the colon separates the true value from the false value, as shown in step 1 of the execution_table.
Is the condition evaluated more than once?
No, the condition is evaluated only once at step 1, then the result decides which value is assigned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the condition result?
Atrue
Bfalse
Cundefined
Derror
💡 Hint
Check the 'Condition (age >= 18)' column at step 1 in execution_table.
At which step is the value 'Adult' assigned to status?
AStep 2
BStep 1
CBefore Step 1
DNever assigned
💡 Hint
Look at the 'Value assigned to status' column in execution_table.
If age was 16, what would be the value assigned to status?
Anil
B"Adult"
C"Minor"
Derror
💡 Hint
Think about the ternary operator logic shown in concept_flow and execution_table.
Concept Snapshot
Syntax: condition ? value_if_true : value_if_false
Evaluates condition once.
Returns value_if_true if condition true.
Returns value_if_false if condition false.
Used for simple if-else in one line.
Full Transcript
The ternary operator in Ruby is a short way to write an if-else statement. It first checks a condition. If the condition is true, it returns the first value after the question mark. If false, it returns the value after the colon. In the example, age is 20, which is greater or equal to 18, so the condition is true. The status variable is assigned 'Adult'. Then, the program prints 'Adult'. This operator helps write simple decisions in one line, making code shorter and easier to read.

Practice

(1/5)
1. What does the ternary operator do in Ruby?
condition ? value_if_true : value_if_false
easy
A. It chooses one of two values based on a condition.
B. It creates a loop that runs twice.
C. It defines a new method.
D. It comments out a line of code.

Solution

  1. Step 1: Understand the ternary operator syntax

    The ternary operator uses a condition followed by a question mark, then the value if true, a colon, and the value if false.
  2. Step 2: Identify its purpose

    This syntax means it picks one of two values depending on whether the condition is true or false.
  3. Final Answer:

    It chooses one of two values based on a condition. -> Option A
  4. Quick Check:

    condition ? true_value : false_value = chooses value [OK]
Hint: Remember: condition ? true : false picks one value fast [OK]
Common Mistakes:
  • Thinking it creates loops
  • Confusing it with method definitions
  • Believing it comments code
2. Which of the following is the correct syntax for a ternary operator in Ruby?
easy
A. condition ? value_if_true : value_if_false
B. condition : value_if_true ? value_if_false
C. condition ? value_if_true ; value_if_false
D. condition ? value_if_true, value_if_false

Solution

  1. Step 1: Recall the ternary operator format

    The correct format is condition followed by a question mark, then the true value, a colon, and the false value.
  2. Step 2: Compare options

    Only condition ? value_if_true : value_if_false matches this exact syntax; others misuse punctuation or order.
  3. Final Answer:

    condition ? value_if_true : value_if_false -> Option A
  4. Quick Check:

    Syntax = condition ? true : false [OK]
Hint: Question mark then colon separate true and false parts [OK]
Common Mistakes:
  • Swapping question mark and colon
  • Using semicolon instead of colon
  • Separating values with commas
3. What is the output of this Ruby code?
age = 20
message = age >= 18 ? "Adult" : "Minor"
puts message
medium
A. Minor
B. 20
C. SyntaxError
D. Adult

Solution

  1. Step 1: Evaluate the condition age >= 18

    Since age is 20, which is greater than or equal to 18, the condition is true.
  2. Step 2: Apply the ternary operator

    Because the condition is true, message is assigned "Adult".
  3. Final Answer:

    Adult -> Option D
  4. Quick Check:

    20 >= 18 ? "Adult" : "Minor" = "Adult" [OK]
Hint: Check condition result first, then pick true or false value [OK]
Common Mistakes:
  • Choosing the false value by mistake
  • Printing the variable instead of its value
  • Confusing syntax errors
4. Find the error in this Ruby code using a ternary operator:
score = 75
result = score > 60 ? "Pass" "Fail"
puts result
medium
A. Wrong variable name
B. Missing colon between true and false values
C. Missing question mark after condition
D. Using double quotes incorrectly

Solution

  1. Step 1: Check ternary operator syntax

    The ternary operator requires a colon ':' between the true and false values.
  2. Step 2: Identify missing colon

    The code has "Pass" "Fail" without a colon, causing a syntax error.
  3. Final Answer:

    Missing colon between true and false values -> Option B
  4. Quick Check:

    Syntax = condition ? true : false [OK]
Hint: Always put ':' between true and false parts [OK]
Common Mistakes:
  • Omitting the colon
  • Using comma instead of colon
  • Forgetting the question mark
5. You want to assign a variable status based on a number n: if n is positive, assign "Positive"; if zero, assign "Zero"; else assign "Negative". Which Ruby code using ternary operators is correct?
hard
A. status = n > 0 ? "Positive" : n == 0 ? "Zero" "Negative"
B. status = n > 0 ? "Positive" : "Zero" ? n == 0 : "Negative"
C. status = n > 0 ? "Positive" : (n == 0 ? "Zero" : "Negative")
D. status = (n > 0 ? "Positive" : "Zero") ? n == 0 : "Negative"

Solution

  1. Step 1: Understand nested ternary operator usage

    To handle three cases, nest one ternary inside the false part of another, using parentheses for clarity.
  2. Step 2: Analyze each option

    status = n > 0 ? "Positive" : (n == 0 ? "Zero" : "Negative") correctly nests the second ternary inside parentheses, ensuring proper evaluation order.
  3. Final Answer:

    status = n > 0 ? "Positive" : (n == 0 ? "Zero" : "Negative") -> Option C
  4. Quick Check:

    Nested ternary with parentheses = correct [OK]
Hint: Use parentheses to nest ternary operators clearly [OK]
Common Mistakes:
  • Missing parentheses causing wrong evaluation
  • Misplacing question marks and colons
  • Confusing order of conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard