7 Things I Love About Sinatra

I first heard of Sinatra a couple of years ago and have been in love with it ever since. For those who haven’t heard of it, Sinatra is a DSL that is used to create web applications. It is written in Ruby and was originally designed by Blake Mizerany, but there’s now a core team who maintain and develop the code. In case you were wondering, it is named after Frank Sinatra, apparently for having

so much class he deserves a web-framework named after him.

Here’s seven reasons why I love Sinatra so much:

Simplicity

You can create a fully functional web app using Sinatra in just one file. There are no complicated set up procedures, configuration, or generators to worry about: You can just open up a text editor and start hacking away. Routes are simple to implement and have a great syntax that uses all of the Http verbs GET,POST,PUT and DELETE; making it a cinch to create RESTful applications. Simplified, Sinatra lets you write simple yet elegant code that produces amazing results.

Flexibility

Sinatra can be used to build anything from the smallest of microsites to a full-scale web application. It’s the perfect choice for API implementations, Middleware, widgets, Facebook apps as well as many others. I often use it to just try out new ideas or prototype a website quickly.

Sinatra is built on top of Rack, making it easy to extend using Ruby Gems, Rack-Middleware and its own extension API. This opens up a world of possibilities for extending your application and avoids you having to reinvent the wheel.

It’s Lightweight

There’s not much in the way of wasted code – basically there is enough to get you going and not much more. This means that it doesn’t have all the bells and whistles that Rails and other frameworks have in terms of helper functions and generator scripts, but it also doesn’t tie you to doing things in a certain way, making it ultra-flexible. When you write an app in Sinatra there will be no wasted features or unused functions, meaning it uses less memory and runs quicker.

Unbiased

You don’t have to use the MVC paradigm and you’re not tied to any particular ORM (Active Record, Sequel, Mongoid etc), JavaScript framework (JQuery, MooTools,YUI etc) or Templating system (Haml, ERB , Slim etc). This puts you, the developer, back in the driving seat, giving you full control over how to best implement your application. This can make Sinatra development seem like the Wild West at times and of course can lead to some bad code … but hey, at least it’s your bad code!

It Helps Increase Your Ruby Proficiency

Everything in Sinatra is just Ruby code, so you get lots of practice while coding up your app. Because there is less ‘magic’ going on in the background, you often have to figure out how to implement certain types of functionality yourself. This isn’t necessarily a bad thing – there’s always someone ready to help (see point 6). And Sinatra doesn’t have it’s own ‘coding style’ like Rails does, things are very much more in the traditional Ruby style. Sinatra’s source code weighs in at just over 1500 lines of well documented code, which means that you can easily read through it yourself. It is clear, concise and contains some great examples of good Ruby programming practice.

The Community is Amazing

Sinatra has a friendly and vibrant community who are always happy to help out. The docs on the official website are also of great value. If you are stuck you can always head over to the Google Group or find somebody on the irc channel (#sinatrarb).

It’s fun!

This is the most important point – Sinatra lets me simply enjoy playing around with code. I can quickly try out new ideas or experiment with different methodologies without any major overhead. I don’t have to follow any guidelines or rules and can just go my own way and get something running in no time at all then enjoy hacking away at it for the rest of the day!

Your First Date With Sinatra

All you need to do is install the Sinatra gem:

 $> gem install sinatra 

 

Then open up a text editor and enter the following 2 lines:

 
require "sinatra"
get("/") { "Hello World!" }

And that is your app – that’s right, just 2 lines! All you need to do now is launch the server. Open up a terminal and type the following at the command line:

 $> ruby hello_world.rb 

 

Your first Sinatra app can be found at http://localhost:4567. Go ahead and have a play around … you’ll fall in love in no time!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://raul.murciano.net Raul Murciano

    Nice article, though looks like the syntax highlighter has eaten some code in the second example…

  • http://paul.annesley.cc/ Paul Annesley

    Sinatra certainly rocks; WordPress less so. It’s doing horrible things to your two-line app example.

  • http://ididitmyway.heroku.com/ Darren Jones

    Thanks for spotting that guys – I’ve corrected it now!

  • Tim

    Using the name Sinatra in conjunction with the image on the website, those guys are asking for a lawsuit. Just a warning.

  • http://ididitmyway.heroku.com/ Darren Jones

    @Tim

    I assume the use of the name and imagery gets filed under ‘homage’. The site has been around for a few years now and doesn’t seem to have caused any offence …

  • Marco

    Why is it that every sinatra tutorial or blog article is only spotting a simple hello world?
    I mean it sure ly is a nice webframework, but i always just see a hello world and are left by myself

    • http://ididitmyway.heroku.com/ Darren Jones

      I hear you Marco!

      Don’t worry, I’m working on some tutorials that cover using Sinatra in more depth. I’m going to start with the basics and build up to full web apps with database backends.