在 RoR 中重新打开课程的位置 [英] Where to reopen a class in RoR
问题描述
我正在尝试重新打开 rails 中的 String
类并添加更多方法供我的应用程序使用.编写代码不是问题 - 我的问题是这段代码应该放在什么地方.
I'm attempting to reopen the String
class in rails and add a bunch more methods for my app to use. Writing the code isn't a problem - my question is rather about where this code should go.
在不同的模型文件中重新打开一个类对我来说没有意义,因为它实际上与任何模型都没有任何关系.我想也许在 config
或 lib
的某个地方会有意义,但我还不是特别精通 RoR.
It doesn't make sense to me to reopen a class inside a different model file, because it really has nothing to do with any of the models specifically. I thought perhaps somewhere in config
or lib
would make sense, but I'm not particularly well versed with RoR yet.
总而言之,定义类修改代码最合乎逻辑的位置是什么,根据代码加载的位置/时间,是否有任何影响?
To summarize, where would be the most logical place to define class-modifying code, and are there any implications depending on where/when the code is loaded?
推荐答案
最合乎逻辑的地方可能是 config/initializers
目录中的一个文件.任何你放入这里的 *.rb
文件都会在 rails 启动时自动执行.如果你愿意,你可以把它们放在一个子文件夹中,这样你就可以做类似 config/initializers/extensions/*.rb
的事情.
The most logical place is probably in a file in the config/initializers
directory. Any *.rb
file you put in here will be automatically executed when rails boots. If you want, you could put them in a sub folder, so you could do something like config/initializers/extensions/*.rb
.
这篇关于在 RoR 中重新打开课程的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!