What if your method names could warn you about surprises before you even run the code?
Why Method naming conventions (? and ! suffixes) in Ruby? - Purpose & Use Cases
Imagine you have many methods in your Ruby program, but none of them clearly tell you if they just check something or if they change data. You have to guess or read the code every time.
This guessing wastes time and causes mistakes. You might call a method thinking it only checks a value, but it actually changes it! This can break your program in ways that are hard to find.
Ruby uses special method name endings: ? for methods that answer yes/no questions without changing anything, and ! for methods that do something risky or change data. This helps you understand what methods do just by their names.
def save # saves data end def save? # unclear if it checks or saves end
def save! # saves data and might raise error end def saved? # clearly checks if saved end
This naming style lets you quickly know if a method is safe to call or if it might change things, making your code easier and safer to use.
When you call user.valid?, you know it just checks if the user data is good. But user.save! will try to save and might raise an error if something goes wrong. This saves you from surprises.
Method names ending with ? mean they check and return true/false without changing anything.
Method names ending with ! mean they do something important or risky, like changing data or raising errors.
This helps you write and read code that is clear and less error-prone.
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
