tags:

views:

39

answers:

1

I've written a gem that looks in config/ for a config file, this bit works fine and its not throwing any problems but if the user changes any config they have to stop the program and start it again before my gem loads the new config, which would require them to restart a rails app every change, which isnt ideal.

is there a way to "re-require" a file so that every run it loads it up fresh instead of using the cached version

+5  A: 

You can use load instead of require. This will load the file regardless of whether it was already loaded before. Note that with load you need to specify the .rb extension which is optional with require. So require "path/to/myconfig" becomes load "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 = true to $verbose = false then $verbose will be false but $debug will still be true after reloading the config.

Of course you'll need to put the load statement somewhere where it will be executed every time the config file should be reloaded (i.e. inside some method or hook).

sepp2k