Ruby variables are invalid in the development mode.

Source: Internet
Author: User

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.

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.