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?
Consider this Ruby method. What will it return when called with
calculate(5)?Ruby
def calculate(x) y = x * 2 y + 3 end calculate(5)
Attempts:
2 left
💡 Hint
Remember, Ruby methods return the value of the last expression automatically.
✗ Incorrect
The method multiplies 5 by 2 to get 10, then adds 3, so the last expression is 13, which is returned implicitly.
❓ Predict Output
intermediate2:00remaining
What does this Ruby method return?
Look at this method and tell what it returns when called with
greet('Alice').Ruby
def greet(name) message = "Hello, " + name message.upcase end greet('Alice')
Attempts:
2 left
💡 Hint
The last expression is
message.upcase, which changes the string to uppercase.✗ Incorrect
The method creates a greeting message and then returns the uppercase version of it because the last expression is
message.upcase.🔧 Debug
advanced2:00remaining
Why does this method return nil?
This method is supposed to return the doubled value of
num, but it returns nil. Why?Ruby
def double(num) result = num * 2 puts result end double(4)
Attempts:
2 left
💡 Hint
Check what
puts returns in Ruby.✗ Incorrect
In Ruby,
puts outputs to the screen but returns nil. Since it's the last expression, the method returns nil.🧠 Conceptual
advanced2:00remaining
What value does this method return?
Given this method, what is the return value when called with
status_check(true)?Ruby
def status_check(flag) if flag "Success" else "Failure" end end status_check(true)
Attempts:
2 left
💡 Hint
The last expression executed depends on the condition.
✗ Incorrect
Since
flag is true, the if branch returns "Success" as the last expression, so the method returns it.❓ Predict Output
expert2:00remaining
What is the output of this Ruby code?
What does this method return when called with
process(3)?Ruby
def process(n) n.times do |i| return i if i == 2 end 10 end process(3)
Attempts:
2 left
💡 Hint
The method uses an explicit return inside a loop.
✗ Incorrect
The method returns 2 immediately when
i == 2 inside the loop, so it never reaches 10.