In application.rb, I have
config.autoload_paths += %W(#{config.root}/lib
So when I modify a class under lib, my code is reloaded.
However, when I tried adding config/initializers to autoload, I noticed my code doesn't get updated.
If I'm writing an extension for the string class, I have to restart rails every time I modify my code.
Please advise?
Initializer files are loaded only once when the rails server is started. Restart the server when initialzers values are changed. For further information see the rails initialization guides.
Auto Reloading 'lib' on change
You can auto reload lib files. Follow Link Autoload and Reload lib directory on change
In Configuring Rails Applications: config.reload_classes_only_on_change
enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to true. If config.cache_classes
is true, this option is ignored.
Initializers are only loaded when starting rails (and never reloaded). When tinkering in config/initializers
you will have to restart rails every time.
Of course, you could make sure your code is defined in /lib
so you can still make sure it works, by using your test-suite.
E.g. in lib/speaker.rb
write
module Speaker
def speak
puts "Ahum, listen: #{self.to_s}"
end
end
and in your initializer you could then do something like
class String
include Speaker
end
While this will still only get loaded when starting rails, you can develop and test your module more easily.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With