Ternary operator usage in Ruby - Time & Space Complexity
We want to see how using the ternary operator affects how long the code takes to run.
Does it change the speed depending on input size?
Analyze the time complexity of the following code snippet.
def check_number(num)
result = num > 0 ? "Positive" : "Non-positive"
result
end
check_number(5)
check_number(-3)
This code uses a ternary operator to decide if a number is positive or not.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single comparison and assignment using the ternary operator.
- How many times: Runs once per function call, no loops or recursion inside.
Each time we call the function, it does one quick check, no matter the number size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations remains constant (1 check) regardless of the input size.
Time Complexity: O(1)
This means the operation takes the same small amount of time no matter what number you check.
[X] Wrong: "The ternary operator makes the code slower because it looks complicated."
[OK] Correct: The ternary operator is just a short way to write an if-else check. It runs just as fast as a simple if-else statement.
Understanding how simple operations like the ternary operator work helps you explain your code clearly and shows you know how code speed relates to what you write.
"What if we replaced the ternary operator with a loop that checks multiple numbers? How would the time complexity change?"
Practice
condition ? value_if_true : value_if_false do in Ruby?Solution
Step 1: Understand the ternary operator format
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.Step 2: Identify its purpose
It returns the first value if the condition is true, otherwise the second value.Final Answer:
It chooses one of two values based on the condition's truth. -> Option AQuick Check:
condition ? true_value : false_value = chooses value [OK]
- Confusing ternary with loops
- Thinking it defines methods
- Misunderstanding the colon as comment
Solution
Step 1: Recall ternary operator syntax
The correct syntax places a question mark after the condition, then the true value, a colon, then the false value.Step 2: Compare options
Only condition ? value_if_true : value_if_false matches the correct format:condition ? true_value : false_value.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 comma
age = 20 puts age >= 18 ? "Adult" : "Minor"
Solution
Step 1: Evaluate the condition
The conditionage >= 18is true because age is 20.Step 2: Apply ternary operator
Since the condition is true, the expression returns "Adult".Final Answer:
Adult -> Option DQuick Check:
20 >= 18 ? "Adult" : "Minor" = "Adult" [OK]
- Choosing the false value by mistake
- Printing the variable instead of result
- Expecting syntax error
score = 85 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_value : false_value [OK]
- Omitting colon
- Using comma instead of colon
- Forgetting question mark
values = [0, 1, nil, 5]
results = values.map { |v| v ? "Yes" : "No" }
puts results.inspectSolution
Step 1: Understand truthiness in Ruby
In Ruby, 0 and 1 are truthy, nil is falsy.Step 2: Apply ternary operator to each element
For each value: 0 (truthy) -> "Yes", 1 (truthy) -> "Yes", nil (falsy) -> "No", 5 (truthy) -> "Yes".Step 3: Check mapped results
The results array is ["Yes", "Yes", "No", "Yes"].Final Answer:
["Yes", "Yes", "No", "Yes"] -> Option CQuick Check:
Truthy values = "Yes", nil = "No" [OK]
- Thinking 0 is falsy
- Confusing nil with false
- Misreading map block
Ready to prove it?
Pass the test and this pattern moves unknown
