Predicate methods (ending with ?) in Ruby - Time & Space Complexity
We want to understand how long it takes for predicate methods to run as the input changes.
Specifically, we ask: how does the time grow when checking conditions with these methods?
Analyze the time complexity of the following code snippet.
def all_even?(numbers)
numbers.all? { |num| num.even? }
end
list = [2, 4, 6, 8, 10]
puts all_even?(list)
This code checks if all numbers in a list are even using a predicate method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over each number in the list to check if it is even.
- How many times: Up to once per element, stopping early if a number is not even.
As the list gets longer, the method checks more numbers, but it may stop early if a number is odd.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows roughly in direct proportion to the list size.
Time Complexity: O(n)
This means the time to check grows linearly with the number of items in the list.
[X] Wrong: "Predicate methods always run in constant time because they just check a condition."
[OK] Correct: Even though the check is simple, the method may need to look at every item in the list, so time grows with input size.
Understanding how predicate methods scale helps you explain your code's efficiency clearly and confidently.
"What if we changed all? to any?? How would the time complexity change?"
Practice
Solution
Step 1: Understand predicate method purpose
Predicate methods in Ruby are designed to answer yes/no questions about an object or data.Step 2: Identify return type of predicate methods
They always return true or false, which are boolean values.Final Answer:
A boolean value (true or false) -> Option BQuick Check:
Predicate methods return true/false [OK]
- Thinking predicate methods return strings or numbers
- Confusing predicate methods with regular methods
- Ignoring the question mark at the end
Solution
Step 1: Recognize predicate method naming
Predicate methods must end with a question mark (?).Step 2: Check method syntax
def valid?; true; end defines a method named valid? correctly with def and end keywords.Final Answer:
def valid?; true; end -> Option AQuick Check:
Method name ends with ? and syntax is correct [OK]
- Omitting the question mark in method name
- Using invalid method name characters like ! or :
- Incorrect method definition syntax
def empty_string?(str)
str.empty?
end
puts empty_string?("")
puts empty_string?("hello")Solution
Step 1: Understand the method behavior
The method empty_string? calls str.empty? which returns true if str is an empty string.Step 2: Evaluate the calls
empty_string?("") returns true because "" is empty; empty_string?("hello") returns false because "hello" is not empty.Final Answer:
true\nfalse -> Option CQuick Check:
Empty string check returns true, non-empty returns false [OK]
- Assuming non-empty strings return true
- Confusing empty? with nil? method
- Ignoring the question mark in method names
def positive?(num)
if num > 0
true
else
false
end
end
puts positive?("5")Solution
Step 1: Analyze method input and comparison
The method compares num > 0, but "5" is a string, not a number.Step 2: Understand Ruby type comparison
Comparing string "5" with integer 0 causes an ArgumentError at runtime.Final Answer:
ArgumentError because "5" is a string, not a number -> Option AQuick Check:
Comparing string and integer causes ArgumentError [OK]
- Passing strings instead of numbers
- Assuming no error on mixed type comparisons
- Missing end keyword (not the case here)
valid_name? that returns true only if a string is not empty and contains only letters. Which implementation is correct?Solution
Step 1: Understand the requirements
The method must return true only if the string is not empty and contains only letters.Step 2: Evaluate each option
def valid_name?(name) !name.empty? && name.match?(/^[a-zA-Z]+$/) end checks that name is not empty and matches only letters using regex /^[a-zA-Z]+$/. This meets the requirements.Final Answer:
def valid_name?(name) !name.empty? && name.match?(/^[a-zA-Z]+$/) end -> Option DQuick Check:
Not empty and only letters = valid_name? true [OK]
- Using || instead of && for conditions
- Checking for digits instead of letters
- Ignoring empty string check
Ready to prove it?
Pass the test and this pattern moves unknown
