Bird
0
0

Find the error in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Classes and Objects
Find the error in this Ruby code:
class Car
  def start
    self.start_engine
  end
  def start_engine
    puts 'Engine started'
  end
end
Car.start
AMissing self in method call
BCalling instance method on class
Cstart_engine method undefined
DSyntax error in method definition
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls

    start and start_engine are instance methods, but Car.start calls start on the class.
  2. Step 2: Understand method call context

    Instance methods cannot be called directly on the class; this causes a NoMethodError.
  3. Final Answer:

    Calling instance method on class -> Option B
  4. Quick Check:

    Instance methods need an instance to be called [OK]
Quick Trick: Instance methods require an object, not class name [OK]
Common Mistakes:
  • Calling instance method on class
  • Assuming self fixes call
  • Ignoring method scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes