Challenge - 5 Problems
Ruby Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple method call
What is the output of this Ruby code?
Ruby
def greet(name) "Hello, #{name}!" end puts greet("Alice")
Attempts:
2 left
💡 Hint
Look at how the method uses string interpolation with the parameter.
✗ Incorrect
The method greet takes a name and returns a greeting string with that name inserted. Calling puts on the method call prints the greeting.
❓ Predict Output
intermediate2:00remaining
Return value of method without explicit return
What does this Ruby method return when called with argument 5?
Ruby
def square(x) x * x end result = square(5) puts result
Attempts:
2 left
💡 Hint
In Ruby, the last evaluated expression is returned by default.
✗ Incorrect
The method multiplies x by itself and returns the result. Calling square(5) returns 25.
🔧 Debug
advanced2:00remaining
Identify the error in method declaration
What error does this Ruby code produce?
Ruby
def add_numbers(a, b) sum = a + b puts sum end add_numbers(3, 4)
Attempts:
2 left
💡 Hint
Check if all method definitions are properly closed.
✗ Incorrect
The method is missing the 'end' keyword to close the definition, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Method with default parameter value
What is the output of this Ruby code?
Ruby
def greet(name = "Guest") "Welcome, #{name}!" end puts greet puts greet("Bob")
Attempts:
2 left
💡 Hint
Look at the default value assigned to the parameter.
✗ Incorrect
If no argument is given, the method uses the default 'Guest'. Otherwise, it uses the provided name.
🧠 Conceptual
expert2:00remaining
Understanding method scope and variable visibility
Consider this Ruby code. What is the output?
Ruby
def change_var(x) x = 10 end value = 5 change_var(value) puts value
Attempts:
2 left
💡 Hint
Think about whether the method changes the original variable or just a copy.
✗ Incorrect
The method changes only the local copy of x, not the original variable 'value'. So 'value' remains 5.