Bird
0
0

Find the error in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Metaprogramming Fundamentals
Find the error in this Ruby code:
MyClass = Class.new do
  def greet
    'Hi'
  end
end
obj = MyClass.new
greet()
Agreet() called without object instance
BClass.new block missing end
CMethod greet missing return statement
DObject instantiation syntax incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Review method call

    Method greet is called without specifying the object obj.
  2. Step 2: Understand scope of method

    Instance methods must be called on an instance; calling greet() alone causes error.
  3. Final Answer:

    greet() called without object instance -> Option A
  4. Quick Check:

    Instance methods require object receiver [OK]
Quick Trick: Always call instance methods on object, not standalone [OK]
Common Mistakes:
  • Calling instance methods without object
  • Forgetting to assign Class.new to constant
  • Assuming implicit self call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes