I have a few methods I am adding to Ruby’s Array class for my Rails application. Where is the best place to put them?
Right now I have them in config/environment.rb.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
config/environment.rb isn’t really the best place, since you can run into serious load ordering-problems if try to extend classes that haven’t been resolved at the time environment.rb is executed.
Better to put a file into config/initializers. Any script placed there will be executed after the rails runtime is loaded.
What you could do is to create a file lib/my_extensions.rb
then in lib/my_extensions/array.rb :
and in config/initializers/load_my_extensions.rb
This will cause MyExtensions::Array to be auto-reloaded each time you invoke a request in development mode. This is nicer than restarting your application everytime you make a change to your code.