问题描述
在 Rails 3 中使用 Monkey Patch 的首选方法是什么?
What is the preferred way to Monkey Patch in Rails 3?
我只想给 String 类添加一个方法.我更在意将文件放在哪里.
I just want to add a method to the String class. I'm more looking at where to place the file.
推荐答案
初始化器目录是收集所有这些小碎片的好地方.由于我倾向于使用核心扩展程序有点过火,我喜欢在那里创建一个名为扩展程序"的文件夹,然后将它们全部放入其中.
The initializer directory is a good place to collect all those little scraps. Since I tend to go a bit overboard with core extensions, I like to make a folder there called "extensions" and toss them all in there.
所以,试试 /config/initializers/string_extension.rb
或 /config/initializers/extensions/string.rb
或类似的东西.无论哪种方式,您都可以在之后忘记它们 - Rails 会为您需要它们,因此您无需自己动手.
So, try /config/initializers/string_extension.rb
, or /config/initializers/extensions/string.rb
, or something similar. Either way, you can just forget about them afterward - Rails will require them for you, so you don't need to do it yourself.
这篇关于Rails 3 中的猴子补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!