Truthy and falsy values (only nil and false are falsy) in Ruby - Time & Space Complexity
We want to understand how checking if a value is truthy or falsy affects the time it takes to run a program.
Specifically, how does the program's work change when it tests values like nil or false?
Analyze the time complexity of the following code snippet.
values = [nil, false, 0, '', true, [], {}]
values.each do |val|
if val
puts "Truthy"
else
puts "Falsy"
end
end
This code checks each value in a list to see if it is truthy or falsy, then prints a message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the array and checking its truthiness.
- 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 work grows directly with the number of values; double the values, double the checks.
Time Complexity: O(n)
This means the time to check truthy or falsy values grows in a straight line with the number of values.
[X] Wrong: "Checking if a value is truthy or falsy takes longer for some values like 0 or empty strings."
[OK] Correct: In Ruby, only false and nil are falsy; all other values, including 0 and empty strings, are truthy and checked in the same quick way.
Understanding how simple checks like truthiness scale helps you reason about code efficiency and write clearer, faster programs.
"What if we changed the array to include nested arrays or hashes? How would the time complexity change?"
Practice
Solution
Step 1: Recall Ruby falsy values
In Ruby, onlyfalseandnilare falsy.Step 2: Check each option
0, empty string, and empty array are all truthy in Ruby.Final Answer:
false -> Option AQuick Check:
Falsy = false or nil [OK]
- Thinking 0 is falsy like in some other languages
- Assuming empty string is falsy
- Confusing empty array with falsy
x is truthy in Ruby?Solution
Step 1: Understand Ruby truthy check
In Ruby,if xautomatically checks ifxis truthy (not false or nil).Step 2: Analyze options
if x == trueonly matches true, missing other truthy values;if x != niltreatsfalseas truthy (false != nil);if x == nilchecks the opposite of truthy.Final Answer:
if x -> Option BQuick Check:
Useif xfor truthy check [OK]
- Using 'if x == true' which misses other truthy values
- Using 'if x != nil' which treats false as truthy
- Checking 'if x == nil' to test truthiness
value = 0 if value puts "Truthy" else puts "Falsy" end
Solution
Step 1: Identify the value and condition
The variablevalueis assigned0. Theif valuechecks ifvalueis truthy.Step 2: Recall Ruby truthy rules
In Ruby,0is truthy, so theifbranch runs.Final Answer:
Truthy -> Option DQuick Check:
0 is truthy in Ruby [OK]
- Assuming 0 is falsy like in other languages
- Expecting else branch to run
- Confusing output with error
var is falsy. What is wrong?
var = nil if var == false puts "Falsy" else puts "Truthy" end
Solution
Step 1: Understand falsy values in Ruby
Bothfalseandnilare falsy in Ruby.Step 2: Analyze the condition
The code checks onlyvar == false, so it missesnilwhich is also falsy.Final Answer:
It misses the case when var is nil, which is also falsy -> Option CQuick Check:
Falsy = false or nil [OK]
- Checking only for false and ignoring nil
- Confusing syntax errors with logic errors
- Using wrong comparison operators
arr = [false, nil, 0, "", [], true]Which code correctly returns only truthy values?
Solution
Step 1: Understand filtering truthy values
Usingselect { |v| v }keeps only truthy values becausevin condition is true for truthy values.Step 2: Analyze other options
arr.select { |v| v != false }keeps nil (nil != false);arr.reject { |v| v }rejects truthy values;arr.select { |v| v == true }keeps only true, missing other truthy values.Final Answer:
arr.select { |v| v } -> Option AQuick Check:
Use select with block returning value for truthy filter [OK]
- Using reject instead of select
- Checking explicitly for false instead of truthiness
- Selecting only true, missing other truthy values
Ready to prove it?
Pass the test and this pattern moves unknown
