Class variables hold data shared by a class and all its subclasses. They let you keep track of information common to all objects of a class.
Class variables (@@) and their dangers in Ruby
class ClassName @@class_variable = value def some_method @@class_variable end end
Class variables start with @@ and are shared by the class and all its subclasses.
Changing a class variable affects all classes in the inheritance chain.
class Animal @@count = 0 def initialize @@count += 1 end def self.count @@count end end class Dog < Animal end Dog.new Dog.new puts Animal.count
class Parent @@value = 1 end class Child < Parent def self.change_value @@value = 5 end end Child.change_value puts Parent.class_variable_get(:@@value)
This program counts all vehicles created, including cars and trucks, using a class variable.
class Vehicle @@total_vehicles = 0 def initialize @@total_vehicles += 1 end def self.total_vehicles @@total_vehicles end end class Car < Vehicle end class Truck < Vehicle end Car.new Car.new Truck.new puts Vehicle.total_vehicles
Class variables can cause unexpected bugs because subclasses share the same variable.
Changing a class variable in one subclass affects all other subclasses and the parent class.
Consider using class instance variables or class methods for safer, clearer sharing of data.
Class variables (@@) share data across a class and all its subclasses.
They can cause tricky bugs because all classes share the same variable.
Use them carefully or prefer safer alternatives like class instance variables.
Practice
@@)?Solution
Step 1: Understand class variable scope
Class variables (@@) are shared across a class and all its subclasses, meaning they hold the same value for all related classes.Step 2: Compare with other variable types
Instance variables are unique per object, local variables are method-specific, so they don't share data like class variables do.Final Answer:
They are shared among a class and all its subclasses. -> Option BQuick Check:
Class variables share data across class hierarchy [OK]
- Confusing class variables with instance variables
- Thinking @@ variables are local to instances
- Assuming @@ variables are private to one class
Solution
Step 1: Identify class variable syntax
Class variables start with@@, so@@count = 0is correct for defining a class variable.Step 2: Differentiate other variable types
@countis an instance variable,$countis a global variable, andcountalone is a local variable.Final Answer:
class MyClass @@count = 0 end -> Option AQuick Check:
Class variable syntax uses @@ [OK]
- Using @ instead of @@ for class variables
- Confusing global variables ($) with class variables
- Defining variables without any prefix
class Parent
@@value = 1
def self.value
@@value
end
def self.value=(v)
@@value = v
end
end
class Child < Parent
end
Parent.value = 5
puts Child.valueSolution
Step 1: Understand class variable sharing
The class variable@@valueis shared betweenParentandChildclasses.Step 2: Trace the assignment and output
SettingParent.value = 5changes@@valueto 5 for both classes. So,Child.valuereturns 5.Final Answer:
5 -> Option AQuick Check:
Class variables shared, so Child.value = 5 [OK]
- Assuming Child has its own separate @@value
- Expecting default value 1 after change
- Confusing class variables with instance variables
class A
@@count = 0
def initialize
@@count += 1
end
def self.count
@@count
end
end
class B < A
def initialize
@@count += 10
end
end
A.new
B.new
puts A.countSolution
Step 1: Analyze class variable usage in inheritance
Both classesAandBshare the same@@count.A#initializeadds 1,B#initializeadds 10.Step 2: Understand the effect on count
CreatingA.newadds 1, thenB.newadds 10, so total@@countbecomes 11, which may be unexpected if counting instances separately.Final Answer:
The class variable @@count is shared and modified differently causing unexpected count. -> Option CQuick Check:
Shared @@count causes tricky bugs [OK]
- Thinking @@count is separate per class
- Ignoring inheritance effect on @@ variables
- Assuming initialize methods don't affect shared vars
@@)?Solution
Step 1: Understand the problem with class variables
Class variables (@@count) are shared across subclasses, causing counts to mix and produce wrong totals.Step 2: Use class instance variables for separate counts
Class instance variables (@count) belong to each class separately, so each subclass can track its own count safely.Final Answer:
Use class instance variables (@count) in each subclass instead of @@count. -> Option DQuick Check:
Class instance vars avoid shared @@ dangers [OK]
- Using global variables which mix counts globally
- Trying to reset @@count which is error-prone
- Using local variables which don't persist counts
Ready to prove it?
Pass the test and this pattern moves unknown
