Why does Ruby always return a value from methods even if no explicit return is used?
hard📝 Conceptual Q10 of 15
Ruby - Methods
Why does Ruby always return a value from methods even if no explicit return is used?
ABecause Ruby methods return the first expression evaluated
BBecause Ruby methods return nil by default if no return is given
CBecause Ruby requires all methods to have a return statement
DBecause Ruby methods implicitly return the last evaluated expression
Step-by-Step Solution
Solution:
Step 1: Recall Ruby method return rules
Ruby methods automatically return the value of the last expression evaluated inside them.
Step 2: Eliminate incorrect options
Options A, B, and C are incorrect because Ruby does not return the first expression, does not return nil by default, and does not require explicit return.
Final Answer:
Because Ruby methods implicitly return the last evaluated expression -> Option D
Quick Check:
Implicit last expression return = Because Ruby methods implicitly return the last evaluated expression [OK]
Quick Trick:Ruby methods return last expression automatically [OK]
Common Mistakes:
Thinking explicit return is mandatory
Assuming nil is returned if no return
Believing first expression is returned
Master "Methods" in Ruby
9 interactive learning modes - each teaches the same concept differently