Challenge - 5 Problems
Ruby Method Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code with a method ending with '?'?
Consider the following Ruby code. What will be printed when calling
even_number??Ruby
def even_number?(num) num % 2 == 0 end puts even_number?(4) puts even_number?(5)
Attempts:
2 left
💡 Hint
Methods ending with '?' usually return true or false.
✗ Incorrect
The method
even_number? checks if a number is even and returns a boolean. For 4, it returns true; for 5, false.❓ Predict Output
intermediate2:00remaining
What does the method ending with '!' do differently?
Look at this Ruby code. What will be the output after calling
capitalize! on the string?Ruby
str = "hello" result = str.capitalize! puts str puts result
Attempts:
2 left
💡 Hint
Methods ending with '!' usually modify the object itself.
✗ Incorrect
The
capitalize! method changes the original string to "Hello" and returns the modified string.🔧 Debug
advanced2:00remaining
Why does this method with '?' suffix raise an error?
This Ruby method is intended to check if a number is positive. Why does it raise a syntax error?
Ruby
def positive?(num) if num > 0 return true else return false end end
Attempts:
2 left
💡 Hint
Check the syntax after the method body ends.
✗ Incorrect
The extra '?' after the 'end' keyword is invalid syntax in Ruby, causing a syntax error.
❓ Predict Output
advanced2:00remaining
What is the output of this code using both '?' and '!' methods?
Given this Ruby code, what will be printed?
Ruby
class User attr_accessor :name def initialize(name) @name = name end def name? !@name.nil? && !@name.empty? end def clear_name! @name = "" end end user = User.new("Alice") puts user.name? user.clear_name! puts user.name?
Attempts:
2 left
💡 Hint
The '?' method checks if name is present; the '!' method clears it.
✗ Incorrect
Initially, name is "Alice", so
name? returns true. After clearing, name is empty string, so name? returns false.🧠 Conceptual
expert2:00remaining
Why should methods ending with '!' be used carefully in Ruby?
Which of the following best explains the purpose and caution behind methods ending with '!' in Ruby?
Attempts:
2 left
💡 Hint
Think about what the '!' means in method names in Ruby conventions.
✗ Incorrect
In Ruby, methods ending with '!' usually modify the object they are called on and may have side effects, so they should be used carefully.