Class variables are used in paging. They are mainly used to mark the ID of the "page number input box ".
If a page has several pages, the ID of the "page number input box" must be different to identify which page is to be paged. The class variable is used to achieve this purpose, so that all object instances share a variable without the need to re-initialize the variable each time.
Sample Code for using class variables
1 require 'ruby-debug' 2 debugger 3 class Myclassvar 4 @@a=1 5 puts 1111111111111111111111 6 puts @@a 7 8 def testa 9 @@a=@@a+5 10 end 11 def testb 12 @@a=@@a+3 13 end 14 end 15 16 class Reclassvar < Myclassvar 17 def testa 18 @@a+=10 19 end 20 end 21 22 obj=Myclassvar.new 23 puts obj.testa 24 puts obj.testb 25 obj2=Myclassvar.new 26 puts obj2.testb 27 obj3=Reclassvar.new 28 puts obj3.testa
The execution sequence is 3 => 4 => 5 => 6 => 8 => 11 => 16 => 17 => 22 => 23 => 9 => 24 => 12 => 25 => 26 => 12 ......
The local Test variables fully conform to the expectation, but the class variables used in the project paging are initialized every time, once depressing. The result is that config. cache_classes = false is included in the configuration of rails in the Development Mode. Therefore, you can view the running result of the updated code every time you do not need to restart the service. The program on the server is enabled in the production mode, where config. cache_classes = true. This is why the class variable fails in the Development Mode. Every time a class is reloaded, all its class variables will be reinitialized.