0
0
Rubyprogramming~10 mins

Why methods always return a value in Ruby - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why methods always return a value in Ruby
Start method call
Execute each line
Last evaluated expression
Return its value
Method call ends with return value
Ruby methods run all lines and return the value of the last expression automatically.
Execution Sample
Ruby
def greet(name)
  message = "Hello, #{name}!"
  message
end

puts greet("Alice")
This method creates a greeting message and returns it automatically as the last line.
Execution Table
StepActionExpression EvaluatedResultReturn Value
1Method greet called with 'Alice'n/an/an/a
2Assign message"Hello, Alice!""Hello, Alice!"n/a
3Evaluate last linemessage"Hello, Alice!""Hello, Alice!"
4Method returnslast evaluated expression"Hello, Alice!""Hello, Alice!"
5puts printsputs greet("Alice")prints Hello, Alice!nil
💡 Method ends after last line, returning the last expression's value automatically.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
name"Alice""Alice""Alice""Alice"
messagenil"Hello, Alice!""Hello, Alice!""Hello, Alice!"
Key Moments - 3 Insights
Why does the method return the value of the last line without an explicit return?
Ruby automatically returns the last evaluated expression in a method, as shown in execution_table step 4.
What happens if the last line is an assignment?
The assignment expression itself returns the assigned value, so the method returns that value (see step 2).
Why does puts return nil even though it prints text?
puts outputs to the screen but returns nil, so the method's return value is separate from what puts prints (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'message'?
A"Hello, Bob!"
B"Hello, Alice!"
Cnil
DAn error
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the method return its value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Method returns' action in execution_table.
If we add 'return message' as the last line, how does the return value change?
AIt returns the value of message explicitly
BIt returns the value of the first line
CIt returns nil
DIt causes an error
💡 Hint
Ruby methods return the last expression; 'return' just makes it explicit.
Concept Snapshot
Ruby methods always return the value of the last expression evaluated.
No need to write 'return' explicitly.
Assignments return the assigned value.
puts prints output but returns nil.
Method ends after last line, returning that value.
Full Transcript
In Ruby, every method returns a value automatically. This value is the result of the last expression evaluated inside the method. For example, if the last line is a variable or a calculation, Ruby returns that value without needing an explicit return statement. Assignments also return the assigned value. When you use puts inside a method, it prints to the screen but returns nil, so the method's return value is separate from what gets printed. This behavior makes Ruby methods simple and clean to write.