Bird
0
0

The following Ruby method is intended to return the string "Done", but it returns nil instead. What is the error?

medium📝 Debug Q14 of 15
Ruby - Methods
The following Ruby method is intended to return the string "Done", but it returns nil instead. What is the error?
def finish
  puts "Done"
end

result = finish
puts result.inspect
AThe method uses <code>puts</code> which returns nil, so method returns nil.
BThe method is missing an explicit <code>return</code> statement.
CThe method should use <code>print</code> instead of <code>puts</code>.
DThe method name is reserved and cannot be used.
Step-by-Step Solution
Solution:
  1. Step 1: Understand what puts returns

    puts prints to screen but returns nil.
  2. Step 2: Identify method return value

    Since puts "Done" is the last line, method returns nil.
  3. Final Answer:

    The method uses puts which returns nil, so method returns nil. -> Option A
  4. Quick Check:

    puts returns nil [OK]
Quick Trick: puts returns nil, so method returns nil if last line is puts [OK]
Common Mistakes:
  • Thinking puts returns the printed string
  • Believing explicit return is always needed
  • Confusing print and puts behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes