Bird
0
0

This code tries to reopen the Integer class to add a method double. What is wrong?

medium📝 Debug Q14 of 15
Ruby - Class Methods and Variables
This code tries to reopen the Integer class to add a method double. What is wrong?
class Integer
  def double
    self * 2
  end
end

puts 5.double()
AMethod double should be defined as a class method, not instance method.
BYou cannot reopen built-in classes like Integer.
CNothing is wrong; it will print 10.
DThe method double uses wrong syntax for multiplication.
Step-by-Step Solution
Solution:
  1. Step 1: Check if reopening Integer is allowed

    Ruby allows reopening built-in classes like Integer to add instance methods.
  2. Step 2: Verify method and usage

    Method double multiplies self by 2, and calling 5.double() returns 10.
  3. Final Answer:

    Nothing is wrong; it will print 10. -> Option C
  4. Quick Check:

    Reopen Integer + instance method works = 10 output [OK]
Quick Trick: Reopen built-in classes freely; instance methods work [OK]
Common Mistakes:
  • Thinking built-in classes cannot be reopened
  • Confusing instance and class methods
  • Assuming syntax error in multiplication

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes