Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Rails console reload modules under lib?

I have a module in my Rails project under lib. I run 'rails c' and do some experimenting in the console. I make a change to the module under lib, type 'reload!' from the console and it doesn't reload the file. I have to quit the console and restart, which is real pain.

Is there a better way to reload that file?

like image 667
Chip Castle Avatar asked Jun 15 '11 16:06

Chip Castle


3 Answers

Try this:

load "#{Rails.root}/lib/yourfile.rb"
like image 107
NullRef Avatar answered Nov 14 '22 14:11

NullRef


In case anyone interested, here's my findings on how to auto-reload require files in Rails without restarting server.

The solution is now available as a Ruby gem require_reloader.

like image 2
Huiming Teo Avatar answered Nov 14 '22 15:11

Huiming Teo


Add the following to config/initializers/reload.rb

class Object
  def reload_lib!
    Dir["#{Rails.root}/lib/**/*.rb"].map { |f| [f, load(f) ] } #.all? { |a| a[1] } 
    # uncomment above if you don't want to see all the reloaded files
  end
end

You can now reload all the files in lib by typing reload_lib! in the console

like image 2
Abdo Avatar answered Nov 14 '22 15:11

Abdo