Bird
0
0

Why might class variables (@@var) cause unexpected behavior in Ruby inheritance?

hard📝 Conceptual Q10 of 15
Ruby - Class Methods and Variables
Why might class variables (@@var) cause unexpected behavior in Ruby inheritance?
ABecause they are unique to each subclass
BBecause they are shared across the entire inheritance chain
CBecause they cannot be accessed by class methods
DBecause they are reset for each instance
Step-by-Step Solution
Solution:
  1. Step 1: Understand class variable scope

    Class variables are shared by a class and all its subclasses, not unique per subclass.
  2. Step 2: Consequence of sharing

    This sharing can cause unexpected changes if subclasses modify the same variable.
  3. Final Answer:

    Because they are shared across the entire inheritance chain -> Option B
  4. Quick Check:

    Class variables shared in inheritance = unexpected behavior [OK]
Quick Trick: Class variables shared by all subclasses [OK]
Common Mistakes:
  • Thinking class variables are unique per subclass
  • Assuming class variables reset per instance
  • Believing class variables can't be accessed by class methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes