Method naming conventions (? and ! suffixes) in Ruby - Time & Space Complexity
We want to understand how the use of method names ending with ? and ! affects the time it takes for Ruby programs to run.
Specifically, we ask: Does adding these suffixes change how long methods take to execute?
Analyze the time complexity of these Ruby methods with ? and ! suffixes.
def contains_digit?(str)
str.each_char.any? { |ch| ch =~ /\d/ }
end
def upcase!(str)
str.upcase!
end
The first method checks if a string has any digit. The second changes the string to uppercase in place.
Look at what repeats inside these methods.
- Primary operation: Iterating over each character in the string for
contains_digit?. - How many times: Once per character until a digit is found or the string ends.
- For
upcase!: The method modifies the string directly without explicit loops here, but internally it processes each character.
As the string gets longer, the time to check or modify it grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 character checks or changes |
| 100 | Up to 100 character checks or changes |
| 1000 | Up to 1000 character checks or changes |
Pattern observation: The work grows roughly in direct proportion to the string length.
Time Complexity: O(n)
This means the time to run these methods grows linearly with the size of the input string.
[X] Wrong: "Methods ending with ? or ! run faster or slower because of their names."
[OK] Correct: The suffixes are just naming conventions. They do not change how Ruby runs the code inside the method.
Understanding that method names with ? and ! are about meaning, not speed, helps you write clear code and explain your choices confidently.
"What if the contains_digit? method used a regular expression match on the whole string instead of checking each character? How would the time complexity change?"
Practice
? is used at the end of method names that return true or false.Solution
Step 1: Understand the meaning of
In Ruby, methods ending with?suffix?return true or false, indicating a boolean check.Step 2: Identify the method that returns a boolean
Among the options,valid?clearly suggests a boolean check, while others do not.Final Answer:
valid? -> Option DQuick Check:
Boolean methods end with ? = valid? [OK]
- Confusing ! with ? suffix
- Using ? for methods that modify objects
- Not using ? for boolean-returning methods
Solution
Step 1: Understand the meaning of
In Ruby, methods ending with!suffix!usually modify the object or perform a 'dangerous' action.Step 2: Identify the method that modifies the object
delete!clearly indicates modification, whiledeletemay not, anddelete?suggests a boolean check.Final Answer:
delete! -> Option BQuick Check:
Modification methods end with ! = delete! [OK]
- Using ? instead of ! for modifying methods
- Not adding ! for dangerous methods
- Confusing method names without suffixes
class User
def active?
true
end
def deactivate!
@active = false
"User deactivated"
end
end
user = User.new
puts user.active?
puts user.deactivate!
puts user.active?Solution
Step 1: Analyze the
This method always returnsactive?methodtruebecause it returns the literaltruewithout checking any variable.Step 2: Analyze the
deactivate!method and secondactive?calldeactivate!sets@active = falsebutactive?does not use@active, so it still returnstrue.Final Answer:
true User deactivated true -> Option AQuick Check:
active? returns true always = true User deactivated true [OK]
- Assuming deactivate! changes active? output
- Confusing method side effects
- Ignoring method return values
def save? # saves the object puts "Saving..." true end
What is wrong with this method name?
Solution
Step 1: Understand the meaning of
Methods ending with?suffix?should return true or false and not perform side effects like saving.Step 2: Analyze the method behavior
This method prints "Saving..." which is a side effect, so it should not end with?.Final Answer:
Method ends with ? but performs an action, not a boolean check -> Option CQuick Check:
Methods with ? should not have side effects [OK]
- Using ? for methods that change state
- Ignoring method side effects
- Confusing ! and ? suffix meanings
Solution
Step 1: Identify boolean check method
The method checking if a string is empty should return true or false, so it should end with?, e.g.,empty?.Step 2: Identify modifying method
The method that clears the string modifies it, so it should end with!, e.g.,clear!.Final Answer:
empty? and clear! -> Option AQuick Check:
Boolean check ends with ?; modifying method ends with ! [OK]
- Swapping ? and ! suffixes
- Not using suffixes for clarity
- Using no suffix for modifying methods
Ready to prove it?
Pass the test and this pattern moves unknown
