In ruby, there are subtle differences between class instance variables and class instance methods and class variables and class methods, and there are also considerable differences in usage. This article discusses their definitions and basic usage scenarios to attract jade
I Class instance variables and class variables
Class variables are familiar to everyone. They are variables that start with @ @ in the class definition. Class variable is used to store the global information of a class. It only belongs to a class. Unlike class instance variables (i.e. variables defined with @ at the beginning), each class object has a piece of data. Class variables can be inherited, that is, if we derive a subclass, we can access the class variables of the parent class in the subclass. Subclasses and parent classes share a piece of data, and modifications to one class will be reflected in another class. As shown in the following code running results:
class A
The # class variable must be assigned a value before accessing, otherwise there will be an “uninitialized class variable” error
@@alpha=123 # Initialize @@alpha
@@beta=456 #Initialize @@beta
@@gamma=789 #Initialize @@gamma
def A.alpha
@@alpha
end
def A.alpha=(x)
@@alpha=x
end
def A.beta
@@beta
end
def A.beta=(x)
@@beta=x
end
def A.gamma
@@gamma
end
def A.gamma=(x)
@@gamma=x
end
def A.look
puts “#@@alpha, #@@beta, #@@gamma”
end
end
class B<A
end
#Initial data
A.look
B.look
#Modify class variables in the parent class
A.alpha=111
A.look
B.look
Operation results:
123, 456, 789
123, 456, 789
111, 456, 789
111, 456, 789
So what is the class instance variable of a class? The class instance variable of a class is a variable defined with @ in or outside the class method (not in the instance method, that is the instance variable of the class). The variable value defined in this way belongs to the class object itself and cannot be inherited by subclasses. The term “class object” may be somewhat confusing. In fact, all objects in ruby are objects. The classes we define and Ruby’s built-in classes are metaclass objects themselves. Therefore, if you want to define class global data that needs to be shared with subclasses, use class variables; However, if you want to define class global data that is only used by the class itself, you can use class instance variables. Another thing to note is that unlike class variables, class instance variables do not require assignment before access, and their value is nil. Let’s look at an example to make it clearer
class A
The class instance variable of # class can be assigned or not assigned before access. If it is not assigned, it is nil
@alpha=123 # Initialize @alpha
@beta=456 #Initialize @beta
@gamma=789 #Initialize @gamma
def A.alpha
@alpha
end
def A.alpha=(x)
@alpha=x
end
def A.beta
@beta
end
def A.beta=(x)
@beta=x
end
def A.gamma
@gamma
end
def A.gamma=(x)
@gamma=x
end
def A.look
puts “#@alpha, #@beta, #@gamma”
end
end
class B<A
end
A.look
B.look
The running results of the code are as follows:
123, 456, 789
, ,
nil