Have you ever used a “class instance variable” in any of your Ruby code?
-
23-08-2019 - |
Question
I can understand why you would need a class variable to keep track of things like the total number of objects that have been instantiated in that class.
And I can understand why you would need an instance variable to store attributes of a particular object in that class.
But class instance variables I just can't seem to justify.
As I understand it, they're just like class variables except that they are not visible to subclasses the way class variables are.
It seems that the uses for this would be quite limited. Or am I wrong? Has anyone found a good use for class instance variables in their code? Or could you give an example of a situation where this type of nuance would be valuable?
Solution
Say you want to count the number of instances of a class (not including subclasses)
class A
@count = 0
@@count = 0
def self.inherited(subclass)
subclass.instance_eval { @count = 0 }
end
def self.num_instances
@count
end
def self.num_subclass_instances
@@count
end
def self.new
@count += 1
@@count += 1
super
end
end
class B < A
end
class C < B
end
A.new
B.new
A.new
B.new
B.new
C.new
A.num_instances #=> 2
A.num_subclass_instances #=> 6
B.num_instances #=> 3
B.num_subclass_instances #=> 6
C.num_instances #=> 1
C.num_subclass_instances #=> 6
You can't use the class variable, since that's shared among all
classes and its subclasses. Note how the changes made to @@count
by B
and C
are reflected in A
, but @count
is not shared.
In general, though, it can be very useful for storing any class-specific settings. _why uses it in Dwemthy's Array to specify initial values for instance attributes, and it comes up a lot whenever doing ruby metaprogramming.
OTHER TIPS
Class variables are shared across all instances of a class, which includes all subclasses. Sometimes this variable across a hierarchy is exactly what is needed, but sometimes you might prefer a different variable for each class. Unlike class variables, class instance variables will take different values for each class object.
Yes, actually I have. This is only slightly modified and trimmed from what I had:
class Widget
# class instance variable pattern
class << self; attr_accessor :color; end
def show_color()
"This widget is #{self.class.color}"
end
end
class WidgetR < Widget
@color = "Russet"
end
class WidgetC < Widget
@color = "Cordovan"
end
class WidgetL < Widget
@color = "Liver"
end
WidgetR.new.show_color #=> "This widget is Russet"
But I'm not sure it is really necessary in the case I used it. I could have just overridden the method. Or provided a color method. Or stored it in a class variable as a Hash. Or even kept a copy in each instance (ok, that one is kinda yech). I'm sure there are other possibilities...
There are a variety of alternatives and the syntax is awkward. Given that I'm guessing the instances where it is the most natural thing to use are probably fairly rare.
It might help you to try to reproduce this behavior with class and instance variables and see that it is difficult to achieve (although it is easy if you define methods, etc).
C.J.