Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Classes and Objects
Identify the error in this Ruby code snippet:
class Cat
  def self.meow
    puts "Meow!"
  end

  def meow
    puts "Meow from instance"
  end

  def self.instance_meow
    meow
  end
end

Cat.instance_meow
AMissing <code>self</code> in instance method definition
BCalling instance method <code>meow</code> inside a class method without an instance
CIncorrect syntax for class method definition
DUsing <code>puts</code> inside methods is not allowed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls inside self.instance_meow

    This is a class method trying to call meow without specifying an instance.
  2. Step 2: Understand method types

    meow without self. is an instance method, so it needs an object to call it on.
  3. Step 3: Identify the error

    Calling an instance method directly inside a class method causes a NoMethodError because no instance is given.
  4. Final Answer:

    Calling instance method meow inside a class method without an instance -> Option B
  5. Quick Check:

    Class methods cannot call instance methods without an object [OK]
Quick Trick: Class methods need an instance to call instance methods [OK]
Common Mistakes:
  • Trying to call instance methods directly inside class methods
  • Confusing self in class vs instance methods
  • Ignoring need for object to call instance methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes