简体   繁体   English

类变量生命周期的红宝石

[英]Class variables lifetime in ruby

I have a class variable named fields which is initiated like this: 我有一个名为fields的类变量,它的初始化如下:

@@fields ||= JSON.parse(@client.run_request(@request_path, 'get', params))["data"]

after the first time the method is called, in another rails request, @@fields still holds the 第一次调用该方法后,在另一个Rails请求中, @@fields仍然保留

When does this value expire? 该值什么时候到期?

class variables do not expire, you can always access them through the class, instance variables can be accessed as long as the object lives, have a read here 类变量不会过期,您始终可以通过类访问它们,只要对象存在,就可以访问实例变量,请在此处阅读

http://thoughts.codegram.com/understanding-class-instance-variables-in-ruby/ http://thoughts.codegram.com/understanding-class-instance-variables-in-ruby/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM