How can/should I include Ruby project file dependencies in config/environment? -
new rubyist here. in lectures on ruby, i've come across various tricks/shortcuts adding file dependencies config/environment
file one's program runs smoothly. has been unclear me of these, when combined, redundant; best practices; , useless and/or wrong , should done long way. clarification appreciated!
the ones i've come across:
require_relative "../lib/test1.rb"
: << '.' & require "lib/test1.rb"
: << '.' & dir['lib/*.rb'].each {|f| require f}
require file.dirname(__file__)
- "require-all" gem
feel free include other ways, too!
config/environment.rb
file rails framework has opinions about. on lifetime of application, positively incented not modify file (if can avoid it). allow easiest possible upgrade path. consider placing initialization in file in config/initializers
.
if code alters behavior of rails in such fundamental way placement in config/initializers
loses potency, or if long-term maintenance of code not concern, i'll consider above. items 2+3 work appending load_path
, not recommend, let alone calling best practice. (adding rails_root/config
may not major issue default, might create difficult debug errors.)
the location of config/environment.rb
hasn't changed in long time, relative requirement (i.e. option #1) fine. require file.dirname(__file__)
nothing (you're requiring directory) worthwhile remember in approach require file.join[file.dirname(__file__), '../lib/your_file.rb']
work same regardless of load path, or working directory concerns. do.
i not use gem this, since behavior of gem change in unpredictable ways, , you're in area rails can make choices inconvenient.
Comments
Post a Comment