Boolean values (true, false, nil) in Ruby - Time & Space Complexity
When working with Boolean values like true, false, and nil in Ruby, it is helpful to understand how fast these checks run.
We want to know how the time to check these values changes as the program runs.
Analyze the time complexity of the following code snippet.
values = [true, false, nil, true, false]
values.each do |val|
if val == true
puts "It's true!"
elsif val == false
puts "It's false!"
else
puts "It's nil or something else"
end
end
This code checks each value in a list to see if it is true, false, or nil and prints a message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the array.
- How many times: Once for each element in the array.
As the number of values grows, the program checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the list gets longer.
[X] Wrong: "Checking true, false, or nil is slow because it involves many conditions."
[OK] Correct: Each check is simple and fast; the main time cost comes from how many items you check, not the conditions themselves.
Understanding how simple checks scale helps you explain your code's speed clearly and confidently in real situations.
"What if we changed the array to a nested array and checked values inside each sub-array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of
In Ruby,nilnilis used to represent the absence of any value, like an empty box.Step 2: Compare with other options
trueandfalseare Boolean values meaning yes or no, but not "no value".0is a number, not a special value for nothing.Final Answer:
nil-> Option AQuick Check:
nilmeans no value [OK]
- Confusing false with nil
- Thinking 0 means no value
- Mixing true and nil meanings
Solution
Step 1: Recall Ruby syntax for Boolean values
Ruby uses lowercasetrueandfalsefor Boolean values.Step 2: Check each option
FalseandFALSEare not valid Ruby keywords.FalseValueis not a keyword but a name.Final Answer:
false -> Option DQuick Check:
Boolean false is lowercase false [OK]
- Using capitalized False or TRUE
- Adding extra words to Boolean values
- Confusing symbols with keywords
value = nil if value puts "Yes" else puts "No" end
Solution
Step 1: Understand how Ruby treats
In Ruby,nilin conditionsnilis treated as false in conditions.Step 2: Follow the if-else logic
Sincevalueisnil, theif valuecondition is false, so theelsebranch runs, printing "No".Final Answer:
No -> Option CQuick Check:
nilacts as false in if [OK]
- Thinking nil is true
- Expecting nil to print itself
- Confusing nil with empty string
if false puts "No" elsif nil puts "Maybe" else puts "Yes" end
Solution
Step 1: Check syntax correctness
The code has matchingif,elsif, andend, so no syntax error.Step 2: Evaluate conditions in order
falseis false,nilis also false, so both conditions fail andelseruns, printing "Yes".Final Answer:
No error, prints "Yes" -> Option BQuick Check:
False and nil skip to else [OK]
- Thinking elsif nil runs
- Expecting syntax error
- Confusing elsif with else
status is either true or nil in Ruby. Which condition correctly does this?Solution
Step 1: Understand the condition requirements
You want to check ifstatusis eithertrueORnil, so use logical OR (||).Step 2: Analyze each option
if status == true || status == nil usesstatus == true || status == nil, which matches the requirement. if status == true && status == nil uses AND, which can never be true for both at once. if status || nil is incorrect syntax and logic. if status != false checks ifstatusis not false, which includes other values too.Final Answer:
if status == true || status == nil -> Option AQuick Check:
Use OR (||) to check either condition [OK]
- Using && instead of ||
- Confusing != false with true or nil
- Writing invalid condition syntax
Ready to prove it?
Pass the test and this pattern moves unknown
