Bird
0
0

Find the error in this Ruby class that attempts to override to_s:

medium📝 Debug Q6 of 15
Ruby - Classes and Objects
Find the error in this Ruby class that attempts to override to_s:
class User
  def to_s
    "User: " + @username
end

u = User.new
puts u.to_s
AMissing <code>end</code> keyword for the to_s method
BUsing + operator instead of interpolation
CCalling to_s on an uninitialized variable
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check method definition

    The to_s method lacks a closing end keyword.
  2. Step 2: Confirm other parts

    Using + for string concatenation is valid. The variable @username may be nil but won't cause syntax error. The object u is instantiated correctly.
  3. Final Answer:

    Missing end keyword for the to_s method -> Option A
  4. Quick Check:

    Syntax error due to missing end [OK]
Quick Trick: Every method must end with end keyword [OK]
Common Mistakes:
  • Forgetting to close method with end
  • Assuming string concatenation causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes