简体   繁体   English

如何在Rails中重新定义/覆盖初始化变量?

[英]How to redefine/override initialized variables in Rails?

One of my applications loads a yml file as a global variable in config/environment.rb so that the variable could be used globally, like this. 我的一个应用程序将yml文件作为全局变量加载到config / environment.rb中,以便可以像这样全局使用该变量。

CFILE = YAML::load(File.open('path/to/the/file')) CFILE = YAML :: load(File.open('path / to / the / file'))

Now I need to periodically reload the file to the same variable, since the file would be periodically modified. 现在,我需要定期将文件重新加载到相同的变量,因为该文件将被定期修改。 I've tried adding a rake task with the same line above but it doesn't really make the change. 我试过在上面的同一行中添加一个rake任务,但实际上并没有做出更改。

How can I update the variable? 如何更新变量?


[Solved] Resolved with CFILE.replace new_cfile [解决]使用CFILE.replace new_cfile解决

You're not supposed to do that. 你不应该那样做。 :) But, you can! :) 但是你可以!

Object.send(:remove_const, :CFILE)

Then redefine as if it were never there. 然后重新定义,好像它从未出现过一样。

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

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