Karel Minarik: How do I learn and master Sinatra?

Welcome to the fourth installment on the RL blog, of a mini series – “How do I learn and master Sinatra?” – by top Rubyists using Sinatra. The interview series will provide insight and commentary from these notable Sinatra developers, with the goal of facilitating and providing answers to the questions Ruby beginners face on how to learn and master Sinatra.

Satish>> Karel Minarik, could you tell us something about yourself – your background, where you are based?

Karel
Minarik Karel Minarik>> I’m Karel Minarik, web designer and developer living in Prague, Czech Republic. I have graduated in Philosophy, not Computer Science, which may explain why I love Ruby a lot, and why I prefer solving “naming things” over “cache invalidation” problems. I earn my bread by designing interfaces, writing Ruby, JavaScript, HTML/CSS and giving people advice or teaching them new tricks. I blog in undecipherable intervals on Restafari.org and publish code regularly at Github.

Satish>> Are there any pre-requisites for a person to start learning Sinatra?

Karel>> Very few: you just need to know Ruby a little bit. The rest you can and will learn along the way. In fact, Sinatra is wonderful teaching tool to deepen your knowledge of Ruby as a general programming language, web application architectures, HTTP and REST principles, concept of middlewares, and so on. As a wonderful teaching/learning tool it’s truly on par with _why’s Shoes.

Satish>> How should one start learning Sinatra?

Karel>> You should start with the README, which contains almost everything you need to know in its 500 or so lines. Then you should definitely glance over sourcecode of some Sinatra applications “in the wild“.

Some of the noteworthy examples would be eg. simple website in waferbaby’s usesthis, background processing tutorial in bmizerany’s sinatra-dj, clever use of Ruby’s blocks/closures in pjhyett’s github-services or ultra minimal apps in ichverstehe’s gaze or gnugeek’s tophat. These examples really elucidate compact and minimal nature of Sinatra.

Then you should sketch something rather small and well defined: web frontend for some Ruby code you have, a web API for some of your services, …

Sinatra – quickly create tiny web apps and services

Satish>> Which area of Sinatra should a beginner pay particular attention to?

Karel>> Beginners should pay attention to Sinatra’s DSL itself: helpers, filters, last_modified and etag support, etc, so they’re not reinventing the mic and truly make use of it’s API. More advanced programmers should focus on Rack integration, using Rack middlewares such as Rack::Auth or Rack::Mime in your Sinatra app and running Sinatra apps themselves as middlewares. This opens different possibilities of service integration – have a look on Jon Crosby’s wonderful explanation in his MWRC talk.

Satish>> Is the official documentation on Sinatra good enough for a beginner? Are there areas which need improvement or need to be re-written

Karel>> Sinatra’s documentation is pretty extensive at the moment, covering everything from basics to testing your applications and writing extensions. It’s just a bit scattered at the moment, eg. deployment is covered in the Sinatra Book started by Chris Schneider. There’s still some lack of thorough documentation about Rack integration.

Satish>> Sequel, DataMapper, ActiveRecord – which one would you recommend to use with Sinatra and why?

Karel>> I prefer ActiveRecord for anything talking to a relational database, because of it’s clever API, stability, general knowledge and large user base. Don’t forget that Sinatra is nice playground for experiments with other ORM’s, key/value stores, etc, though!

Sinatra
Icon

Satish>> Is an understanding of Rack important while learning Sinatra? Why? Which area of Rack should one be really comfortable with?

Karel>> No, you could start learning Sinatra completely oblivious of something called “Rack”.

However, you can use plethora of various bundled or third-party Rack middlewares very easily by simple ‘use Rack::Utils‘ or ‘use Rack::Locale‘ declaration for adding some advanced functionality to your application.

And when you plan to plug Sinatra powered app into a Rails one, for instance, or want to “mount” various separated web applications at different endpoints, you should definitely have a detailed look on Rack itself.

Satish>> How should one hone one’s skills in Sinatra?

Karel>> By reading huge amounts of code available on Github. That’s a sure way how to discover clever solutions and open your mind. (Be sure to include credits if you reuse some code and release your stuff, though.)

But the most important thing is to focus on Ruby as an expressive programming language, and to _not_ think about browser first. Think first about the domain of your application and how it translates to Ruby, not about how it should “look” or behave in a browser. That’s very important, but comes next. And don’t forget it’s really easy to code test-first in Ruby and in Sinatra.

Satish>> What type of projects should a beginner work on to gain more expertise in Sinatra?

Karel>> Smallish apps, where Rails would force it’s conventions on you or which are not primarily focused on database access. Something like cschneid’s irclogger, quirkey’s columnlog or entp’s Calendar About Nothing — all very tight, minimal and very elegant apps.

Satish>> Could you suggest some web services that a Sinatra beginner could develop himself / herself?

Karel>> The sweet spot for Sinatra is something along the lines of already mentioned apps. Some ideas I could throw in:

  • An app to display metrics about your team activity in a Git repository: who commited most, who commited most lines of code, etc., leveraging power of Grit.
  • A web frontend for some command-line tool like ‘top’ or ‘df’ for your servers.
  • Simple web hook for Github’s post-receive hooks, notifiying your developer mailing-list, Jabber, deploying new code to staging server or playing a tune.
  • More advanced example could be an app to show currently deployed versions of your applications, using small Sinatra apps on each host to emit various metrics like deployed revision and it’s age, system load, etc in JSON and a Sinatra app to gather the data — “emulating” services like NewRelic, Scout or FiveRun’s Dash.

Satish>> Anything else you would like to add?

Karel>> Do come to the #sinatra IRC channel on Freenode when you get stuck. There’s usually lots of people from different timezones, so it’s very likely that we’ll get you out of trouble fast. Just please read the README first and don’t name your application file “sinatra.rb” :) Have fun with Ruby and Sinatra!

Thank you Karel. In case you have any queries and/or questions, kindly post your questions here (as comments to this blog post) and Karel would be glad to answer.

Others in this series:

Post supported by 1st Easy Limited: UK based 1st Easy Limited offer Sinatra and Rails hosting running on a Phusion Passenger (mod_rails) and LAMP stack. If you want to try your hand at developing with Sinatra, why not let them arrange a trial hosting account for you? You’ll get to deploy your app, with full technical support from their team!

comments powered by Disqus