4 Reasons to Try Sinatra

Share this article

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:

[gist id=”4197373″]

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:

[gist id=”4197383″]

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!

Andy HawthorneAndy Hawthorne
View Author

Andy Hawthorne is from Coventry in the UK. He is a senior PHP developer by day, and a freelance writer by night, although lately that is sometimes the other way around.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week