Automatically reload rail module

I am developing a ruby ​​module that I include in my rails application. I want it to restart automatically when it changes. I did an extensive Google search, and looked at various issues here that discuss this, but they all seem outdated or incorrect.

How to get an external module to reload in rails when changing it? I tried adding his name to ActiveSupport::Dependencies.unloadable_constants , but after I typed reload! in the console, I can no longer refer to this character without NameError: uninitialized constant foo , even if I do another require 'foo_module' . Does anyone know how to make this work?

Note: here is one possible duplicate , but pay attention to the comments on the “answer” that it never solved the problem for the modules. There is also this question , which has a dead link in the answer, and finally this , which also doesn’t solve it.

+3
ruby-on-rails
Jan 24 '10 at 23:55
source share
3 answers

I found how to do this:

  • Make sure FooModule is in lib/foo_module.rb .
  • Use require_dependency to request an external library in lib/foo_module.rb .

These steps are necessary, and others are not required.

+4
Jan 26
source share

There are two separate issues here.

Simply put, you use require if you want to load .

  • require will evaluate the code in the file once, no matter how many times this file / module is required.

  • load will evaluate the code in the file each time the file is uploaded.

require preferable to use the load so that the files are not evaluated several times when many files are hanging on them.

The short option is that load can be used to reload modules that have already been loaded with require .




A more difficult problem is to automatically reload the module when changing.

One of the possible duplicates listed in the question is a link to here . Which implies a prefix of any code that depends on your module with the conditional load of your module if it changes after loading. You will need to use a global variable to track file downloads.

NB: this should not be used on a production server, but should be good on a development server or console.

+1
Jan 25
source share

I sometimes did research on this problem.

Here are my findings on how to automatically reload required files in Rails without restarting the server.

The solution is now available as the Ruby require_reloader gem.

0
Jan 10 '13 at 10:29
source share



All Articles