I’ve written a gem that looks in config/ for a config file. This works fine but if the user changes any config they have to stop the program and start it again before my gem loads the new config. This would require them to restart the whole rails app on every change, which isn’t ideal.
Is there a way to “re-require” a file so that it loads it up fresh instead of using the cached version.
You can use
loadinstead ofrequire. This will load the file regardless of whether it was already loaded before. Note that withloadyou need to specify the.rbextension which is optional withrequire. Sorequire "path/to/myconfig"becomesload "path/to/myconfig.rb".Note that this will not undefine anything defined by the previous config. So if the config is changed from
$verbose = true; $debug = trueto$verbose = falsethen$verbosewill befalsebut$debugwill still betrueafter reloading the config.Of course you’ll need to put the
loadstatement somewhere where it will be executed every time the config file should be reloaded (i.e. inside some method or hook).