Bird
0
0

Why does self inside a class method refer to the class object and not an instance in Ruby?

hard📝 Conceptual Q10 of 15
Ruby - Classes and Objects
Why does self inside a class method refer to the class object and not an instance in Ruby?
ABecause class methods are singleton methods on the class object
BBecause Ruby treats all methods as instance methods
CBecause self always refers to the main object
DBecause class methods are inherited from superclass
Step-by-Step Solution
Solution:
  1. Step 1: Understand class methods nature

    Class methods are singleton methods defined on the class object itself, not on instances.
  2. Step 2: Explain self binding

    Inside class methods, self refers to the object the method is called on, which is the class object.
  3. Final Answer:

    Because class methods are singleton methods on the class object -> Option A
  4. Quick Check:

    Class method self = class object due to singleton method [OK]
Quick Trick: Class methods are singleton methods on class object [OK]
Common Mistakes:
  • Thinking self is always main object
  • Confusing inheritance with self binding
  • Assuming all methods are instance methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes