Ternary operator in Ruby - Time & Space Complexity
We want to see how using a ternary operator affects how long a program takes to run.
Does it change the speed depending on input size?
Analyze the time complexity of the following code snippet.
def check_number(n)
result = n > 0 ? "Positive" : "Non-positive"
result
end
check_number(5)
check_number(-3)
This code checks if a number is positive using a ternary operator and returns a string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single comparison and assignment using the ternary operator.
- How many times: Exactly once per function call, no loops or repeated steps.
Execution time stays the same no matter the input size because it only does one check.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 comparison and 1 assignment |
| 100 | 1 comparison and 1 assignment |
| 1000 | 1 comparison and 1 assignment |
Pattern observation: The number of operations does not increase as input size grows.
Time Complexity: O(1)
This means the time to run stays the same no matter how big the input is.
[X] Wrong: "Using a ternary operator makes the code slower because it looks complicated."
[OK] Correct: The ternary operator is just a shortcut for an if-else and runs in constant time, so it does not slow down the program.
Understanding that simple operations like the ternary operator run in constant time helps you explain your code clearly and shows you know how to think about efficiency.
"What if we replaced the ternary operator with a loop that checks multiple conditions? How would the time complexity change?"
Practice
condition ? value_if_true : value_if_falseSolution
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.Step 2: Identify its purpose
This syntax means it picks one of two values depending on whether the condition is true or false.Final Answer:
It chooses one of two values based on a condition. -> Option AQuick Check:
condition ? true_value : false_value = chooses value [OK]
- Thinking it creates loops
- Confusing it with method definitions
- Believing it comments code
Solution
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.Step 2: Compare options
Only condition ? value_if_true : value_if_false matches this exact syntax; others misuse punctuation or order.Final Answer:
condition ? value_if_true : value_if_false -> Option AQuick Check:
Syntax = condition ? true : false [OK]
- Swapping question mark and colon
- Using semicolon instead of colon
- Separating values with commas
age = 20 message = age >= 18 ? "Adult" : "Minor" puts message
Solution
Step 1: Evaluate the condition age >= 18
Since age is 20, which is greater than or equal to 18, the condition is true.Step 2: Apply the ternary operator
Because the condition is true, message is assigned "Adult".Final Answer:
Adult -> Option DQuick Check:
20 >= 18 ? "Adult" : "Minor" = "Adult" [OK]
- Choosing the false value by mistake
- Printing the variable instead of its value
- Confusing syntax errors
score = 75 result = score > 60 ? "Pass" "Fail" puts result
Solution
Step 1: Check ternary operator syntax
The ternary operator requires a colon ':' between the true and false values.Step 2: Identify missing colon
The code has "Pass" "Fail" without a colon, causing a syntax error.Final Answer:
Missing colon between true and false values -> Option BQuick Check:
Syntax = condition ? true : false [OK]
- Omitting the colon
- Using comma instead of colon
- Forgetting the question mark
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?Solution
Step 1: Understand nested ternary operator usage
To handle three cases, nest one ternary inside the false part of another, using parentheses for clarity.Step 2: Analyze each option
status = n > 0 ? "Positive" : (n == 0 ? "Zero" : "Negative") correctly nests the second ternary inside parentheses, ensuring proper evaluation order.Final Answer:
status = n > 0 ? "Positive" : (n == 0 ? "Zero" : "Negative") -> Option CQuick Check:
Nested ternary with parentheses = correct [OK]
- Missing parentheses causing wrong evaluation
- Misplacing question marks and colons
- Confusing order of conditions
Ready to prove it?
Pass the test and this pattern moves unknown
