Bird
0
0

Why does defining a method with self.method_name inside a Ruby class create a class method instead of an instance method?

hard📝 Conceptual Q10 of 15
Ruby - Class Methods and Variables
Why does defining a method with self.method_name inside a Ruby class create a class method instead of an instance method?
ABecause methods with <code>self.</code> are private by default.
BBecause <code>self</code> always refers to the current instance.
CBecause <code>self</code> inside the class body refers to the class itself, not instances.
DBecause Ruby treats all methods with dots as class methods.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the meaning of self inside a class body

    Inside a class definition but outside any instance method, self refers to the class object itself.
  2. Step 2: Explain how this affects method definition

    Defining a method with self.method_name creates a method on the class object, making it a class method.
  3. Final Answer:

    Because self inside the class body refers to the class itself, not instances. -> Option C
  4. Quick Check:

    self in class body = class object [OK]
Quick Trick: Inside class body, self means the class, so methods become class methods [OK]
Common Mistakes:
  • Thinking self always means instance
  • Assuming self. methods are private
  • Believing dot notation always means class method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes