Bird
0
0

Find the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Advanced Metaprogramming
Find the error in this Ruby code:
class Car
  def initialize(model)
    @model = model
  end
end

car = Car.new('Tesla')
car.class_eval do
  def model_name
    @model
  end
end
Aclass_eval called on instance instead of class
BMissing end keyword for class
CInstance variable @model is not accessible
DSyntax error in method definition
Step-by-Step Solution
Solution:
  1. Step 1: Identify where class_eval is called

    The code calls class_eval on car, which is an instance, not a class.
  2. Step 2: Understand class_eval usage

    class_eval should be called on the class (Car), not on an instance.
  3. Final Answer:

    class_eval called on instance instead of class -> Option A
  4. Quick Check:

    class_eval on instance = error [OK]
Quick Trick: Use class_eval on class, not instance objects [OK]
Common Mistakes:
  • Calling class_eval on instance
  • Assuming instance variables inaccessible
  • Ignoring correct block syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes