4 Reasons to Try Sinatra

Tweet

Sinatra bridges the gap between complex, multi-tiered frameworks, and simplicity. If you don’t have a software development education (I don’t) need to develop for the web on a professional basis (I do), then Sinatra offers everything you need.

The list of reasons to give Sinatra a try could be a very long one, but many of the reasons boil down to a number of key points. I’ll run through them here.

1. It’s Easy To Set Up And Maintain

Web development is something I need to do, but it’s not my whole career. That means, I don’t have much time to spend configuring things.

PHP is popular partly because you can have it up and running on your local setup in no time. There are dedicated software binaries available to take the pain out of configuration for most operating systems.

Well, Ruby, and therefore Sinatra, now has that option too. And Ruby Version Manager (RVM) has been around for quite some time for Unix. There is a Windows installer available for Ruby too, so getting the stuff you need for having a go with Sinatra is no longer a problem.

2. It Stays Out Of Your Way – Until You Need It

I remember building my first Sinatra app. Having previously built web apps with PHP frameworks, and a couple of attempts with Rails, I loved the fact that a Sinatra project begins with a single file.

Then I spent the next hour making noises like ‘wow’ and ‘cool’ and other geeky utterances. What was causing that? Things like Sinatra just knowing:

  • About my layout file, just because I created a folder views and a file called layout.erb.
  • Where my assets like images and stylesheets were. All I had to do was create a public folder, and throw them in there.
  • That I wanted my content to appear at the point where I have added <%= yield %> in my layout. Aha, plenty of frameworks do that though. Well, alright, how about being able to load a view partial: <%= erb :'partials/twitter' %>.

I could go on, but I’m sure you get the idea.

What happens when you need to do something more than create static files? Well, Sinatra steps up to the plate. For example, I’m a big fan of Markdown. How do you use it with Sinatra? Simple. Require a gem such as rdiscount, and then in your application Ruby file, you just add:

Assuming that test_file is a Markdown file, it will be rendered as HTML in your browser. It can be argued that the simplicity here is achieved by the flexibility of the rdiscount gem. True, but as with a lot of features you might want to add to your app, Sinatra provides a solid foundation to make it easy to implement.

3. You Can Be Productive Without Being An Expert

Sinatra has some great documentation to begin with. But if you’ve done any kind of web development before, there are certain things that work how you might expect. Or, by using the Ruby gem ecosystem you can get things working by reading the documentation for a gem.

Take file uploading, a fairly common requirement for a web app.

The best place to host Sinatra apps is on Heroku. But Heroku doesn’t use a file system, so you’d be better off uploading somewhere else, like to your Amazon S3 account for example.

There is a Ruby gem for that, called aws-s3. It makes uploading from your Sinatra app straightforward.

Again, it can be argued that it’s the gem that is providing the feature, but it’s the way it can be seamlessly integrated with Sinatra that I really like. And it doesn’t require copious amounts of code – another Sinatra feature.

The way I’ve got along with Sinatra is to wait for a problem, and then have a go at solving it. For example, I was writing this article for Rubysource, and wasn’t sure how to traverse a directory, because I’m still learning Ruby. Since Sinatra is Ruby aware, all I had to do was learn how to do it with Ruby and that gave me the answer for Sinatra.

That might sound like I’m stating the obvious; Sinatra is written in Ruby after all. But try using each of the popular PHP frameworks. They all implement common web app features in a different way. It’s like having to learn PHP all over again when you switch frameworks.

That might not sound like much, but in truth, it is a major redeeming feature of Sinatra. You can literally “learn as you go”.

4. You Get Results – Quickly

Sinatra doesn’t do verbose. Or put another way, if you’re writing lots of code for a Sinatra app, either it’s a very large application, or you’re doing it wrong.

The workflow you can use with Sinatra is streamlined. Once you have your layout, it is literally just a case of creating the methods…

Wait a minute though. In a Sinatra ruby file (I always call mine main.rb), the actions that we add are actually method calls. That’s because Sinatra’s base class has public methods that match the HTTP verbs such as GET, POST, PUT.

So the famous example app:

gives you a strong indicator to how quickly you can get something working. But there is an even better example. You can use Rack::Test with Sinatra. That means that unit testing is easy to implement. There is a reason I’m making a big deal about that. Having spent quite some time developing with PHP, I can tell you from bitter personal experience that trying to get unit testing set up is a pain.

That’s why a lot of PHP developers tend to avoid it. It simply isn’t a problem with Sinatra. You create your main application file, and then another for testing. All you need is the rack-test gem installed. The Sinatra documentation has a great example of how to implement it. Suffice to say, like everything with Sinatra, it’s quick and easy to set up. This leaves you free to think about how you want to test your app.

Finally…

I hope I’ve given you food for thought. I shied away from Sinatra, Rails, and Ruby for a long time. Being self-taught in web development had me thinking that I ought to leave it to those with real expertise.

That’s an insult to the creators of Sinatra. I realise that now. It’s design makes building robust web applications very easy. And of course, it is backed by the power of Rack. It’s my belief that Sinatra grows with you, providing encouraging little nudges to try something new.

Don’t forget that SitePoint is releasing Jump Start Sinatra as a part of it’s Jump Start series of books in early 2013. Make sure you sign up to be notified of its release!

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.

  • naquad

    Sinatra is good until you need something more than a dead simple form / service. Whenever you start to pull in database, testing, etc… you’ll have to search for gems, google for existing integration solutions and finally even reading its source.

    I’ve had a JSON-RPC service written with Sinatra and plain SQL via DBI. It was ok until I’ve had a need for ACL, caching and output filters.

    So if you have something small then Sinatra could be your choice, if you have something bigger than couple of selects – think again. Preliminary optimization is evil, but thinking about what you could need in a couple of month is definitely not bad.

    • http://sidlr.wikia.com han

      dead wrong
      Sinatra is good until you need things done for you.

      Because Sinatra is so slim and addresses only the basic, most basic services that go into a HTTP service – it doesn’t hinder you from configuring any persistence or development-time mechanism (AR, mongoid, rspec, etc…)

      as you clearly agreed “you’ll have to search for gems, google for existing integration solutions and finally even reading its source.”
      yknow…
      knowledge isn’t a bad thing, sometimes we confuse ignorance and abstraction

      “until I’ve had a need ” to know how “ACL, caching and output filters.” actually works – and after I did this in Sinatra I knew much, much better!

      by all means use some (obese) MVC
      I in fact recommend Rails for all noobs

      but after a certain point after 10-20 selects,
      even doing Rails requires a lot of the evaluation, hacking, internals etc…

      imho

      • http://daz4126.com/ Darren Jones

        I agree with han – Sinatra does everything it has to and when you need more there are gems to do that for you. This way, you only ever have to add the actual gems you need and get to stay much more in control of how your application develops. Much better than all the extra bloat you get with other frameworks that you hardly ever need.

        DAZ

  • Erik Straub

    Great article. Sinatra is awesome.

    I know if was only an example, but for file uploads, I’d recommend the fog gem (https://github.com/fog/fog) over a platform-specific cloud service gem.

  • http://daz4126.com/ Darren Jones

    Great article Andy, nice one!

    I am in a similar situation to you, being self-taught and found Sinatra to be really easy to pick up for beginners and has the same instant gratification as PHP.

    Sinatra also can also be used to build big, modular, database-driven sites in any style of architecture you choose, making it a great choice, whatever level you’re working at.

    You are only limited with what you can do by Ruby … and Ruby can do pretty much most things!

    DAZ

  • http://sallyport.heroku.com jonty

    My site has been running for about 3 years now with virtually 0 downtime, 0 unhandled exceptions and provides everything my Rapper Sword Dancing team need.

    And yes, it is a Sinatra site.

    At the time I had to hack Carrierwave to use european buckets on S3 but I think the libraries have matured now and you have several choices.

    When youtube changed the way their urls were formateed it only took me about 10 minutes to adapt the code and get it running once again. Many thanks to Heroku for the platform and so far 0 cost apart from a few pence a month for storing files on S3.

    It was my first complete web site and I still would recommend sinatra for small to medium applications.

    All you need is a bit of JS and some good css, say twitter bootstrap, and your site can also look and behave really well.

    Unlike this site which just complained I was posting my comment too quickly(??!!)