Ruby on Rails 服务器上的常量全局对象
使用 Rails 3.2.1 和 Ruby 1.9.3,哪里是初始化全局常量对象的正确位置,以便在 Rails 服务器启动时仅初始化一次。
现在我将其声明为实例对象,并且每次调用该方法时都会对其进行初始化:
@object_wanted_to_be_global_const = Gemname::GemnameClass.new 'input'
将其声明为全局常量变量的最佳位置在哪里?
如果声明为全局变量而不是实例,这将如何影响性能,因为几乎每个请求都会访问该变量?
Working with Rails 3.2.1 and Ruby 1.9.3, where is the proper place to initialize a Global constant object such that it is only initialized once when the rails server is started.
Right now I am declaring it as instance object as and it is initialized every time the method is called:
@object_wanted_to_be_global_const = Gemname::GemnameClass.new 'input'
Where is the best place to declare this as a global constant variable?
If declared as a global instead of an instance, how will this affect performance as the variable is accessed on almost every request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其放入初始值设定项中。
为了尊重 Ruby 的惯例,请将整个名称大写。
我看不出此方法有任何性能问题。
Put this in an initializer.
And to respect Ruby's convention, capitalize the whole name.
I can't see any performance issue regarding this method.