Any?, all?, none? predicates in Ruby - Time & Space Complexity
When using predicates like any?, all?, and none? in Ruby, it's important to understand how the time to run them changes as the list gets bigger.
We want to know how many steps the program takes when checking these conditions on a list.
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = numbers.any? do |num|
num > 5
end
puts result
This code checks if any number in the list is greater than 5.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over the list elements one by one.
- How many times: Up to the entire list length, but may stop early if condition is met.
As the list gets bigger, the number of checks grows, but sometimes it stops early.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Between 1 and 10 checks |
| 100 | Between 1 and 100 checks |
| 1000 | Between 1 and 1000 checks |
Pattern observation: The checks can stop early if the condition is met, so the time can be less than the list size, but in the worst case it checks every item.
Time Complexity: O(n)
This means the time to check grows linearly with the list size in the worst case.
[X] Wrong: "These predicates always check every item in the list no matter what."
[OK] Correct: Actually, any?, all?, and none? stop checking as soon as they know the answer, so they often do less work than the full list length.
Understanding how these predicates work helps you write efficient code and explain your reasoning clearly in interviews.
"What if we changed any? to all?? How would the time complexity change when the condition is rarely true?"
Practice
Which Ruby method returns true if at least one element in a collection meets a condition?
Solution
Step 1: Understand the meaning of
any?any?returns true if at least one element satisfies the condition.Step 2: Compare with other methods
all?requires all elements to satisfy,none?requires none to satisfy, andeach?is not a Ruby predicate method.Final Answer:
any? -> Option BQuick Check:
any? = at least one true [OK]
- Confusing any? with all?
- Thinking none? means at least one
- Using each? which is not a predicate
Which of the following is the correct syntax to check if all elements in an array are even numbers?
numbers = [2, 4, 6, 8]
# Choose the correct line below
Solution
Step 1: Identify the method to check all elements
To verify every element meets a condition, useall?.Step 2: Confirm the block checks evenness
The block{ |n| n.even? }correctly tests if each number is even.Final Answer:
numbers.all? { |n| n.even? } -> Option AQuick Check:
all? checks every element [OK]
- Using any? instead of all?
- Using none? which checks for no matches
- Using each? which is not a predicate
What is the output of the following Ruby code?
words = ["apple", "banana", "cherry"]
result = words.none? { |w| w.length > 6 }
puts result
Solution
Step 1: Evaluate the block condition for each word
Check if any word has length greater than 6: "banana" (6 letters) is not >6, "cherry" (6 letters) is not >6, "apple" (5 letters) is not >6.Step 2: Determine if none? returns true or false
Since no word has length > 6,none?returns true. But "banana" and "cherry" have length exactly 6, not greater than 6.Step 3: Re-examine condition carefully
None have length > 6, sonone?returns true.Final Answer:
true -> Option AQuick Check:
none? means no elements match condition [OK]
- Confusing none? with any?
- Miscounting string lengths
- Assuming none? returns false if some equal condition
Identify the error in this Ruby code snippet:
numbers = [1, 3, 5, 7]
puts numbers.all? { |n| n % 2 == 0 }
What is the output and why?
Solution
Step 1: Analyze the array and condition
The array contains odd numbers: 1, 3, 5, 7. The condition checks if each number is even (n % 2 == 0).Step 2: Evaluate all? with the condition
Since none of the numbers are even,all?returns false.Final Answer:
false -> Option DQuick Check:
all? returns false if any element fails condition [OK]
- Thinking all? returns true if some elements match
- Expecting SyntaxError for correct block usage
- Confusing output with nil
You have a list of user ages: ages = [12, 17, 20, 25, 30]. You want to check if none of the users are under 18 years old. Which Ruby code correctly performs this check?
Solution
Step 1: Understand the condition to check
We want to confirm no user is under 18, meaning no age < 18.Step 2: Choose the method that returns true if no elements match condition
none?returns true if no elements satisfy the block condition.Step 3: Verify the block condition
The block{ |age| age < 18 }checks if age is under 18.Final Answer:
ages.none? { |age| age < 18 } -> Option CQuick Check:
none? means no elements match condition [OK]
- Using any? which checks if some match
- Using all? which requires all to match
- Reversing the condition inside none?
Ready to prove it?
Pass the test and this pattern moves unknown
