Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Deceptive Simplicity: Sinatra Turns 1.0

Deceptive Simplicity: Sinatra Turns 1.0

This item in japanese

Since it appeared in late 2007, the lightweight Sinatra web framework has attracted enough interest that it now ranks first in Google search results for "Sinatra," above any results for Frank or Nancy. InfoQ spoke with Sinatra project founder Blake Mizerany to learn more about Sinatra and what the 1.0 release brings.

Mizerany explained that what makes 1.0 a milestone is the items that were removed, rather than added:

Templates now use Tilt, which took out some large percentage of the lines of code. The Extensions API was solidified. The parts of the API that had been deprecated in the 0.9 series are now completely gone.

Otherwise, the codebase is much the same as the previous release.

Unlike Rails, Sinatra is not a Model-View-Controller (MVC) framework; rather, it is described as "a domain-specific language for creating web applications using Ruby". It provides a simple way to define how an application will respond to HTTP requests following the REST paradigm:

Sinatra is a direct mapping to HTTP. Sinatra is about exposing simplicity instead of hiding complexity. HTTP isn't hard; HTML isn't hard. There is no need to bury it. ... People are fooled by how 'basic' it looks until they try it, then they are hooked.

Sinatra applications can be tiny enough to fit into one file. Each route is defined by an HTTP method and a URL pattern, and is handled by a block. A Sinatra "Hello world" application, for example, looks like this:

require 'rubygems'
require 'sinatra'
get '/hi' do
  "Hello World!"

Sinatra can be used with a variety of different templating systems such as Erb, Erubis, and HAML. However, it does not bundle many other features of MVC frameworks such as form builders and database connection layers. This simplicity allows it to run on a variety of Ruby implementations such as Ruby 1.8.x and 1.9, JRuby, and MacRuby.

The Sinatra in the Wild page chronicles the adoption of Sinatra, listing a variety of web applications and sites using the framework. These include GitHub and Gemcutter, along with companies such as Heroku, Engine Yard, and ENTP. Recently NewRelic announced support for Sinatra in its RPM Ruby Agent performance monitoring tool.

Version 1.0 has introduced some new features. Sinatra's use of the Tilt framework for template rendering adds support for template caching, for consistent template backtraces, and for new template engines. Erb, Erubis, and Haml templates are now compiled the first time they're rendered, resulting in a 5-10x improvement in render time. Furthermore, a new flag controls whether template files are reread from disk and recompiled on each request.

Current users should find little problem upgrading, and will need to do so to continue to get new features. According to the FAQ:

If you've kept up with deprecations and use officially documented features, your app should be fine under Sinatra 1.0. The latest Sinatra 0.9.x release... includes comprehensive deprecation warnings for all incompatibilities introduced in Sinatra 1.0.
There will be additional Sinatra 0.9.x releases for security issues or major defects. No new features will be added to the 0.9.x series.

One reason the 1.0 release has been long in coming is its adoption of the Semantic Versioning Model:

All 1.x releases will be backward compatible with the initial 1.0 release.... We're required not to break anything shipped in 1.0 for a very long time, so interfaces we don't like require considerable scrutiny.

With regard to the future, Mizerany predicts that Sinatra will continue to gain adherents:

Sinatra's simplicity allows you to take it where you like. We want to keep that simplicity so we're very cautious of what goes in. ... We don't have a secret IRC or Campfire room where we decide Sinatra's next steps in a vacuum. It's all open.

Mizerany is particularly pleased with Sinatra's support for extensions (such as the ease with which they are created) saying "There are no specific boilerplate directories or init scripts, they're just libraries that can be vendored or required as gems." A variety of extensions has been written for Sinatra, including ones for interacting with databases via ActiveRecord or Sequel ORM, for authentication, and for asynchronous tasks. Furthermore, Sinatra sits on top of the standard Rack interface for Ruby web frameworks, allowing interaction with Rack middleware components.

Sinatra's ideas have inspired a number of similar frameworks, e.g., Sammy for Javascript and Compojure for Clojure, as well as others in Scala, PHP, .NET, and additional languages.

Development of Sinatra is supported by Heroku:

Sinatra's success came from the community. Heroku has simply given me the time to support it when needed and encouragement to keep going. The community takes 99.9999% of the credit here. Every time I see a thread/question on the mailing list it's already been answered well and politely by someone before I see it.

Rate this Article