Talk about class variables in Ruby

Source: Internet
Author: User

The class variables in Ruby, a lot of articles are not recommended, the main reason is that some of his features are prone to make some mistakes, especially in the wide use of meta-programming.

The initial contact class variable may feel that he and C + + class static members and Java static variables, but in the actual use of a inattention will fall into the trap of class variables

Trap 1, the class variable is different from the class instance variable, the class instance variable is associated with self, but the class variable is associated with the current class scope

classcendclassDEndclassC @ @var="C"  defD.getvar @ @var endendclassD @ @var="D"  defC.getvar @ @var Endendc.getvar#= "D"D.getvar#= "C"

In this example, I define the method of D in Class C, the method of C is defined in D, and the method of C returns the class variable of D, and the method of D returns the class variable of C, because the class variable is not related to the self of the current environment, but only in relation to the class scope of the environment, if not in class keyword , it is considered to be in object.

It feels a little strange, but the more strange is still behind.

Trap 2, superclass if you add a class variable with the same name as a subclass, the class variable of the subclass will be polluted

We know that the inheritance of classes is of a referential nature, and that in a reference tree subclasses can override methods and variables of a superclass without affecting the superclass, but this is not true for class variables.

The class variable has only one entity in the entire inheritance tree, first, if there is a class variable in the superclass, the subclass inherits completely, and the class variable in the subclass does not affect the superclass in principle, but once the same class variable is added to the superclass, the class variable of class will be rewritten automatically!!

classC @ @var1="c_var1"  defGetVar @ @var1 Enddefsetvar @ @var2="c_var2"EndEndclassD <C @ @var2="d_var2"  defGetVar @ @var2 ENDENDC=c.newd=D.NEWP C.getvar#"C_var1"P D.getvar#"D_var2"C.setvarp C.getvar#"C_var1"P D.getvar#"C_var2"

As you can see, subclasses and superclass have their own instance functions of the same name, but they share an identical class variable name.

Trap 3, similar to trap 2, if a class variable is defined in object, the same name class variable in all classes is polluted

Similarly, if there are two identical class variables in two different subclasses, this would have been undisturbed, but once their common superclass defined the class variable, the three class variables would have been forced to unify!!

And we know that the class common superclass is object, so once you define a class variable in object, it will cause all classes to be contaminated with the same class variable!!

classC @ @var="C_var"  defGetVar @ @var endendclassD @ @var="D_var"  defGetVar @ @var ENDENDC=c.newd=D.NEWP C.getvar#"C_var"P D.getvar#"D_var"classObject @ @var="Object_var"End#@ @var = "Main_var"#using this sentence has the same effect, but will get a warningP C.getvar#"Object_var"P D.getvar#"Object_var"

Class variables in an inheritance tree can only have one, do not allow rewriting, but in different inheritance tree, but allow the name of the same, this wonderful thing is still less touch the good

So be careful with class variables, especially don't use monkey patches to define class variables, I even think that this strange thing should be removed from Ruby.

Talk about class variables in Ruby

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.