Bird
0
0

What is a major risk when using Ruby class variables (@@) in inheritance?

easy📝 Conceptual Q1 of 15
Ruby - Class Methods and Variables
What is a major risk when using Ruby class variables (@@) in inheritance?
AThey are shared across the entire inheritance chain, causing unexpected value changes.
BThey are private to each instance and cannot be accessed by subclasses.
CThey automatically reset to nil when a subclass is created.
DThey can only store integer values.
Step-by-Step Solution
Solution:
  1. Step 1: Understand class variable scope

    Class variables in Ruby are shared among a class and all its subclasses, meaning one change affects all.
  2. Step 2: Identify the risk in inheritance

    This sharing can cause unexpected behavior if subclasses modify the variable, impacting other classes.
  3. Final Answer:

    They are shared across the entire inheritance chain, causing unexpected value changes. -> Option A
  4. Quick Check:

    Class variable sharing = unexpected changes [OK]
Quick Trick: Class variables share state across subclasses, beware side effects [OK]
Common Mistakes:
  • Thinking class variables are instance-specific
  • Assuming subclasses have separate copies
  • Believing class variables reset automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes