Adding Automatic Code Reloading to Rails Console

So I have no idea why the rails console in development mode doesn’t already do this, since the whole idea of the console in development mode is for us to mess around and test random stuff in development mode, right? Anyway, I got inspired and helped by Jkfill with his post on Automatic Code Reloading in Rails Console, so here goes, more for my records than yours since you should follow the instructions in his post. I am using Rails 3.2.9, so I had to copy the code from the reload! method in the rails console code, which is not exactly the best practice, but it’s just for dev mode and it works :-)

 #this should be in config/initializers/irb_reloading.rb
    if defined?(IRB::Context) && !defined?(Rails::Server) && Rails.env.development?

  class IRB::Context
    def evaluate_with_reloading(line, line_no)

        ActionDispatch::Reloader.cleanup!
      ActionDispatch::Reloader.prepare!
      evaluate_without_reloading(line, line_no)
    end
    alias_method_chain :evaluate, :reloading
  end

  puts "=> IRB code reloading enabled"
end

In the meanwhile, shortly browsed the code at the Rails gihub project and it’s really a nice way to learn more about rails… must do it more often.

Comments