Reloading changed files during development with Sinatra

While developing Sinatra applications, to reload changed files I’ve been stopping the server (Ctrl C) and restarting it (Rackup), wasting precious time with every change. Today I finally went ahead and added the Reloader extension. Here is how:

Install the gem on your machine (I didn’t add it to the Gemfile since I don’t need it on production):

$ gem install sinatra-contrib

For the simple wedding website that I am working on, a classic Sinatra application, I’m simply requiring it through:

require "sinatra/reloader" if development?

For the modular application that I’m building at work, I added the following code to require it:

require "sinatra/reloader"

class MyApp < Sinatra::Base
  configure :development do
    register Sinatra::Reloader
  end
end

That’s it! Either application will now reload the changed files with every incoming request. If you need to restart the HTTP server on every request check out shotgun.

Sources:
What happened to reloading in Sinatra 0.9.2?: http://www.sinatrarb.com/faq.html#reloading
Sinatra::Reloader: http://www.sinatrarb.com/contrib/reloader
Shotgun: http://rtomayko.github.com/shotgun/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>