Challenge - 5 Problems
Ruby Return 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 method with explicit return?
Consider the following Ruby method. What will be printed when
example_method is called?Ruby
def example_method return 5 10 end puts example_method
Attempts:
2 left
💡 Hint
Remember that
return immediately exits the method with the given value.✗ Incorrect
The
return 5 line stops the method and returns 5. The line 10 is never reached.❓ Predict Output
intermediate2:00remaining
What does this method return without an explicit return?
Look at this Ruby method. What value does it return when called?
Ruby
def no_return_method 3 + 4 end puts no_return_method
Attempts:
2 left
💡 Hint
In Ruby, the last evaluated expression is returned if no explicit return is used.
✗ Incorrect
The method returns the result of
3 + 4, which is 7, because Ruby returns the last expression automatically.🔧 Debug
advanced2:00remaining
Which option causes a syntax error due to incorrect return usage?
Identify which code snippet will cause a syntax error in Ruby because of incorrect use of
return.Attempts:
2 left
💡 Hint
Look for incomplete expressions after the return keyword.
✗ Incorrect
Option A has an incomplete expression after return (5 +) causing a syntax error. Others are valid.
🧠 Conceptual
advanced2:00remaining
What is the effect of an explicit return inside a block?
What happens if you use
return inside a block passed to a method in Ruby?Attempts:
2 left
💡 Hint
Think about how return behaves inside blocks versus methods.
✗ Incorrect
In Ruby, a return inside a block returns from the method that contains the block, not just the block itself.
🚀 Application
expert2:00remaining
What is the output of this Ruby method with multiple returns?
Analyze this Ruby method and determine what it prints when called.
Ruby
def multi_return(x) if x > 10 return 'Greater' elsif x == 10 return 'Equal' end 'Smaller' end puts multi_return(10)
Attempts:
2 left
💡 Hint
Check the condition that matches the input and which return is executed.
✗ Incorrect
Since x is 10, the second condition matches and returns 'Equal'.