Bird
0
0

Why do Ruby methods always return a value even if there is no explicit return statement?

easy📝 Conceptual Q11 of 15
Ruby - Methods
Why do Ruby methods always return a value even if there is no explicit return statement?
ABecause Ruby methods must always have a <code>return</code> statement.
BBecause Ruby methods return the first line's value automatically.
CBecause Ruby methods return <code>nil</code> by default if no value is given.
DBecause Ruby automatically returns the value of the last executed line in the method.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Ruby method return behavior

    Ruby methods do not require an explicit return statement to send back a value.
  2. Step 2: Identify what value is returned

    The value of the last executed line inside the method is automatically returned.
  3. Final Answer:

    Because Ruby automatically returns the value of the last executed line in the method. -> Option D
  4. Quick Check:

    Last line value returned [OK]
Quick Trick: Remember: last line in method is returned automatically [OK]
Common Mistakes:
  • Thinking a return statement is always needed
  • Believing methods return nil if no return is used
  • Assuming the first line's value is returned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes