I was recently asked by my boss that if I had to choose a Ruby web framework, which one would I choose. This is what I found.

I have only written toy examples in Rails (along time ago) and the same goes for Sinatra (except I’ve looked at it more recently). However, I’m quite familiar with two frameworks in the Python world called Django (which is roughly equivalent to Rails) and Bottle (which is inspired by Sinatra).

Short answer: Really an unfair comparison. Rails is a big framework that provides a lot of features while Sinatra is stripped down and aims at handling HTTP request/responses. Rails use case is for a big website or one that is intended to become big. Sinatra’s sweet spot is small websites or a simple apis. When it comes to choosing a complete website framework, a more equal comparison would be between Rails, Padrino or Ramaze.

Longer answer:
For me the most compelling distinction between the two frameworks is the amount of code each has. If I check out the github repo for Sinatra and Rails and run cloc over them, I get the following:

Project Files Blank Comment Code
Sinatra 53 1516 358 8268
Rails 1745 32740 33770 152274

With Sinatra having about 5% of the code of Rails, it should be clear that there’s no way it could hope to be as featureful as Rails. They are therefore addressing very different goals. According to Sinatra’s maintainer:

“…Rails is a framework focused on writing model driven web applications, Sinatra is a library for dealing with HTTP from the server side.”

This is pretty clear from examples where Rails starts off with scaffolding, introducing directory structure and setting up a database, while Sinatra immediately dives in with routing and an explicit GET request.

Because Rails is opinionated software, a lot of design decisions about how to construct a web application has been made for you. In contrast, Sinatra is just a wrapper around Rack and the request/response cycle. All those design decisions e.g. ORM, Ajax, REST, how to handle DB migrations still have to be made when developing with Sinatra. However this can be to be Sinatra’s advantage. Because there is so little code and ‘magic’, this does mean that Sinatra is really fast. Sinatra can be up to 4 times faster than Rails.

What’s more, you don’t need feel like you’ve made the wrong choice and feel hamstrung by Rails if it gets too complicated. In Rails 3.1, you can embed Sinatra within your Rails application.

Ultimately, I think the comparison is probably unfair, given the different goals of the two particular frameworks. I think in terms of full-featured web frameworks, a more equal comparison can be made between Rails, Padrino or Ramaze. These frameworks intend to achieve the same goals but with differing philosophies. The interesting thing about Padrino is that it is built on top of Sinatra. If you don’t like Rails and Sinatra is too stripped down, Padrino represents a nice middle ground in having a lot of the features you need in a web framework but also being as performant as Sinatra.