We use an explicit return statement to clearly say what value a method should give back when it finishes.
Explicit return statement in Ruby
Introduction
Syntax
Ruby
def method_name # some code return value_to_return end
The return keyword immediately ends the method and sends back the value.
If you don't use return, Ruby automatically returns the last evaluated expression.
Examples
return.Ruby
def greet(name) return "Hello, #{name}!" end
Ruby
def check_number(num) return "Too small" if num < 10 "Large enough" end
Ruby
def add(a, b) sum = a + b return sum end
Sample Program
This program checks if a number is even. It uses return to send back a message early if the number is even.
Ruby
def find_even(number) return "Even number found!" if number.even? "Not an even number" end puts find_even(4) puts find_even(7)
Important Notes
Using return can make your code clearer, especially in longer methods.
Remember, return stops the method immediately, so code after it won't run.
Summary
Explicit return tells Ruby exactly what value to send back from a method.
It helps stop the method early and makes your code easier to understand.
You can use it anytime you want to return a value before the method ends.
Practice
1. What does the
return keyword do in a Ruby method?easy
Solution
Step 1: Understand the purpose of
Thereturnreturnkeyword in Ruby is used to send a value back from a method immediately.Step 2: Recognize method behavior with
When Ruby seesreturnreturn, it stops running the method and gives back the specified value.Final Answer:
It immediately sends a value back and stops the method. -> Option BQuick Check:
Explicit return stops method and returns value = B [OK]
Hint: Return sends value back and stops method immediately [OK]
Common Mistakes:
- Thinking return prints output
- Confusing return with variable creation
- Believing return pauses without returning
2. Which of the following is the correct way to explicitly return the value 10 from a Ruby method?
easy
Solution
Step 1: Check Ruby syntax for return
Ruby allowsreturnwith or without parentheses, soreturn(10)is correct syntax.Step 2: Identify invalid options
return10lacks space (syntax error),return = 10is invalid assignment, andreturn: 10uses wrong colon syntax.Final Answer:
return(10) -> Option DQuick Check:
Correct explicit return syntax = C [OK]
Hint: Use return with parentheses for explicit clarity [OK]
Common Mistakes:
- Using assignment syntax with return
- Using colon instead of parentheses
- Omitting parentheses but expecting error
3. What will be the output of this Ruby code?
def example return 5 10 end puts example
medium
Solution
Step 1: Analyze the method
The method first executesexamplereturn 5, which immediately stops the method and returns 5.Step 2: Understand unreachable code
The line10after the return is never reached or executed.Final Answer:
5 -> Option AQuick Check:
Return stops method early, output = 5 [OK]
Hint: Return stops method; code after return is ignored [OK]
Common Mistakes:
- Thinking code after return runs
- Expecting last line value as output
- Confusing return with print
4. Identify the error in this Ruby method:
def check_number(num)
if num > 0
return 'Positive'
else
'Negative'
end
return
endmedium
Solution
Step 1: Trace the if branch
Ifnum > 0,return 'Positive'exits the method immediately.Step 2: Trace the else branch
Else block evaluates'Negative'as last expression (implicit return).Step 3: Check post-conditional code
The finalreturn(nil) is unreachable as if-else covers all paths.Final Answer:
The method has unreachable code after return. -> Option AQuick Check:
Complete if-else leads to unreachable code [OK]
Hint: Check all paths return a value explicitly [OK]
Common Mistakes:
- Assuming last return runs always
- Thinking return inside if is wrong
- Ignoring implicit returns
5. You want a method that returns the first even number from an array or nil if none found. Which code correctly uses explicit return to stop early?
def first_even(numbers)
numbers.each do |n|
if n.even?
return n
end
end
nil
endhard
Solution
Step 1: Understand method logic
The method loops over each number and checks if it is even.Step 2: Analyze explicit return usage
When it finds an even number, it usesreturn nto immediately return that number and stop the method.Step 3: Check fallback return
If no even number is found, the method returnsnilafter the loop.Final Answer:
Returns first even number or nil, stops early with return. -> Option CQuick Check:
Explicit return stops loop and returns value early = A [OK]
Hint: Use return inside loop to exit method early when condition met [OK]
Common Mistakes:
- Thinking return inside block causes error
- Expecting all even numbers returned
- Ignoring nil fallback return
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
