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.
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?
The most logical place is probably in a file in the
config/initializersdirectory. Any*.rbfile 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 likeconfig/initializers/extensions/*.rb.