Have you ever used a "class instance variable" in any of your Ruby code?

I can understand why you need a class variable to keep track of things like the total number of objects that were created in this class.

And I can understand why you need an instance variable to store the attributes of a specific object in this class.

But class instance variables I just can't justify.

As I understand it, they are similar to class variables, except that they are not visible to subclasses like class variables .

It seems that the use of this would be very limited. Or I'm 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?

+7
scope inheritance ruby class instance-variables
source share
3 answers

Suppose 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 cannot use a class variable since it is used for all classes and subclasses. Note how changes made to @@count on B and C are reflected in A , but @count not used.

In general, however, this can be very useful for storing any class-specific parameters. _why uses it in the Dwemthy Array to indicate the initial values ​​of the instance attributes, and this brings a lot whenever metaprogramming ruby ​​is performed.

+4
source share

Class variables are shared across all instances of a class that includes all subclasses. Sometimes this variable in the hierarchy is exactly what you need, but sometimes you may prefer a different variable for each class. Unlike class variables, class instance variables will take different values ​​for each class object.

http://martinfowler.com/bliki/ClassInstanceVariable.html

+3
source share

Yes, actually I have. This is only slightly modified and cut back 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 if this is really necessary if I used it. I could just override the method. Or a color method is provided. Or save it in a class variable as a hash. Or even saved a copy in each copy (well, this is one of them). I am sure there are other possibilities ...

There are many alternatives, and the syntax is inconvenient. Given that I suggest that cases where this is the most natural thing are probably quite rare.

This can help you 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.).

Cj

+3
source share

All Articles