Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Ruby - Metaprogramming Fundamentals
Identify the error in this code snippet:
MyClass = Class.new do
  def greet
    puts 'Hello'
  end
end
obj = MyClass.new
greet
ACalling greet without receiver causes error
BClass.new syntax is invalid
CMethod greet is not defined
DObject instantiation is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check method call context

    The method greet is defined inside the class, but called without an object.
  2. Step 2: Identify error cause

    Calling greet alone outside class context causes a NameError.
  3. Final Answer:

    Calling greet without receiver causes error -> Option A
  4. Quick Check:

    Method must be called on object instance [OK]
Quick Trick: Call methods on object instances, not standalone [OK]
Common Mistakes:
  • Calling instance methods without object
  • Misunderstanding Class.new syntax
  • Assuming greet is global method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes