These suffixes help you understand what a method does just by its name. They make your code easier to read and safer to use.
Method naming conventions (? and ! suffixes) in Ruby
def method_name? # returns true or false end def method_name! # modifies the object or is 'dangerous' end
Methods ending with ? should always return true or false.
Methods ending with ! usually change the object or do something risky.
def empty? @items.empty? end
def save! # saves data and raises error if it fails end
? for methods that answer a question about the object.def valid? # returns true if data is valid, false otherwise end
! for methods that modify the object or have important side effects.def update! # updates the object and may raise an error end
This program shows a class with a ? method to check if the name is empty and a ! method to change the name to uppercase.
class User attr_accessor :name def initialize(name) @name = name end def name_empty? @name.empty? end def upcase_name! @name.upcase! end end user = User.new("") puts user.name_empty? # true user.name = "alice" puts user.name_empty? # false user.upcase_name! puts user.name # ALICE
Not all methods with ! are dangerous, but they usually change the object.
Ruby does not enforce these rules, but following them helps others understand your code.
Use ? only for methods that return true or false.
Use ? at the end of method names that return true or false.
Use ! at the end of method names that modify the object or are 'dangerous'.
These conventions make your code easier to read and safer to use.
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
