Sinatra: Some Questions from Ruby Newbies

by on August 14, 2013

Darren Jones in his excellent book Jump Start Sinatra says “Since its release in 2007, Sinatra has quickly gained in popularity in the Ruby web community due to its elegant simplicity and classy syntax. Everybody who uses it falls in love with its elegant simplicity and classy syntax.”

RubyLearning will be conducting a “free” (i.e. pay if you like) online course on Sinatra from 7th Sept. 2013 and many of the would-be participants (mostly Ruby newbies) would have a plethora of questions related to Sinatra.

Satish Talim of RubyLearning.org talked to Ruby Gurus Andy Lindeman, Carlo Pecchia, Dan Mayer, Darren Jones, Nathan Esquenazi and Sudarshan Shubakar to answer some of the would-be participant’s questions.


Satish>> A warm welcome to you all. For the benefit of the would-be Sinatra course participants, could each one of you tell us something about your self?

Andy LindemanAndy>> I am a software generalist focused on web and mobile. I like open source and work primarily on RSpec. I work at Big Nerd Ranch, primarily writing web application backends in Ruby and Rails. I dabble a good bit: currently Objective C/iOS, Clojure, Erlang, and client-side JavaScript frameworks are on my radar. I like working on open source and meeting new folks in the community.

Carlo PecchiaCarlo>> I am an IT engineer mainly interested on agile methodologies and “good practices” for developing large and complex systems. I am also interested in web architectures and emerging programming languages.

Dan MayerDan>> I am a tech lead on the LivingSocial consumer web team. I have been developing Ruby applications since 2007 and often work with both Rails and Sinatra. I believe in trying to keep code small, and breaking applications up into logical components and services. My thoughts on development can be found on http://mayerdan.com. I am on twitter as @danmayer

Darren JonesDarren>> I am the author of Jump Start Sinatra, a short book that helps you to get up to speed with Sinatra over a weekend, published by Sitepoint. I have been using Sinatra since 2009 and used it to build the Cards in the Cloud website. I live in Manchester in the UK where I teach Mathematics and enjoy playing water polo.

Nathan EsquenaziNathan>> I am one of the creators of the Padrino framework, enabling powerful extensions to the Sinatra core and the co-founder of CodePath, providing practical training to engineers interested in learning mobile development.

Sudarshan ShubakarSudarshan>> I am a software developer based out of Pune, India. I enjoy working with design principles and patterns, learning new programming languages and a bit of open source development. I am majorly a Java developer for the past 11 years and have recently started working on Ruby out of interest.

Satish>> What is Sinatra best suited for?

Andy>> Simple web apps, especially those that are mostly APIs or only have a few views.

Carlo>> Sinatra is an “essential” framework for Ruby based web application. Talking about “web app” the point of reference is – without any doubt – RubyOnRails (RoR): Sinatra is lightweight compared to it. You have less features, but also less stuff to digest to start with. Personally I’ve used Sinatra (and I was really satisfied by it) for apps where data complexity and user interactions are not critical points (in my case: internal enterprise app).

Dan>> Sinatra is great for smaller applications and apis. Sinatra fits really well when there is a limited front end which uses a lot less of the standard view layer helpers and other features common in Rails. I have personally found that Sinatra is best for APIs and micro sites. If an application with a significant front end is going to be developed, it is often best to start with rails as you will start to home grow and try to replace many common features of rails with your own bad implementations. Finally people often forget how much rails helps in terms of basic security precautions, making it harder to make common mistakes. There is a well known Ruby community quote that I can’t find attribution for, “every Ruby web framework eventually becomes a horrible bad and buggy implementation of rails”.

Darren>> Putting your Ruby code onto the web! At its heart Sinatra is essentially just a wrapper for making it easy to deal with HTTP requests and responses by providing some very nice helper methods. This means that it is suited for anything that you can write in Ruby. It can handle small projects brilliantly but is also great for larger projects too.

Nathan>> Sinatra is incredibly well-suited for building REST APIs for mobile clients as well as web back-end services that are paired with modern javascript frameworks like Backbone, AngularJS, Ember, et al.

Sudarshan>> The Sinatra book introduction says it best by these statements:
* In Sinatra, you can write short ad hoc applications or mature, larger application with the same easiness.
* Sinatra really shines when used for experiments and application mock-ups or for creating a quick interface for your code.
My take on this is that Sinatra gets you started quickly on whatever problem you are trying to solve (with a web/URL based solution) quickly. There is hardly a learning curve and you can have a meaningful implementation of your solution within no time.

Another point where Sinatra is most useful is when you would like finer control over how your application is organized. This discussion on stackoverflow.com provides some good inputs on how Sinatra compares to Rails. As one person says in this discussion, there is no limit to what you can do with it.

Satish>> I am a Ruby newbie and if I learn Sinatra, does that help me with Ruby on Rails?

Andy>> I think it depends on exactly how much you know.

For folks with little background on programming or web application development at all, I think it’s a great fit. There’s not very much “magic” and it’s easy to get up and running quickly.

Carlo>> Sure! Learning Sinatra gives you all the details about how HTTP protocol is handled; how routing works; how to manage your models; how to talk with a Data Base (noSQL are welcome too); how a template system has to be used; and so on. My suggestion is: start with Sinatra, and *then* consider RoR for “not small” projects.

Dan>> Learning Sinatra is a great way to begin to learn Rails. It will definitely help you learn Rails as it helps you learn Ruby basics as well as common Ruby web development practices. In many ways it is a smaller and simpler way to learn to work with a web framework, which is excellent. I find while this is great for learning, it leads to a less natural project structure and uncommon patterns for larger applications. Actually learning some of the basic web development concepts first in Sinatra would help one to understand why these concepts exist and why they are bit more complicated in Rails.

Darren>> A little, but it is more useful for improving your Ruby. I think that it would help you understand some of the ‘magic’ that Rails does in the background as you have a lot more control with Sinatra.

Nathan>> Sinatra is an excellent introduction to the Ruby language and to web development. Sinatra has a remarkably short learning curve and as such allows for new developers to tinkering with web apps of all sorts with minimal overhead or confusion. The distinct advantage of learning Sinatra while you are developing your Ruby acumen is that Sinatra is very explicit and simple allowing the expressiveness of Ruby itself to shine through.

Sudarshan>> Hmm, will Sinatra help one learn Ruby on Rails… probably not in an obvious manner. However my understanding is that if one tries to build a well-structured MVC application with Sinatra, one would find it easier to appreciate what Rails offers out of the box and why it mandates what it does.

With Sinatra there is almost no mandate apart from the dsl syntax itself. So you are free to organize your application any way you like.

If you are trying to learn Rails while you are developing your pet application, my understanding is that you may find it slightly difficult because you may need to deviate from your problem domain to understand the framework. This probably isn’t true if you are just trying to learn Rails with just a mock application.

With Sinatra, like I’ve already mentioned, there is hardly a learning curve and you stay focused on your problem domain.

Satish>> How much Ruby do I have to cover in order to start learning Sinatra?

Andy>> I think it again depends on what an individual developer’s background is.

Developers who are new to Ruby but know other object-oriented languages will likely pick up enough Ruby to be proficient very quickly.

Other developers may need more basics about the structure and basic syntax and semantics of programming languages first.

That said, I think you could pair a lot of the learning: while introducing a Sinatra concept, also introduce some Ruby concepts.

Carlo>> At the very beginning “not too much”, but if and when you want to code seriously you have to dirty your hands. There is no escape! :-)

Dan>> I would make sure to cover some Ruby basics firsts:

* Running a ruby script
* variables
* method calls
* basic data structures (array, hashes, strings, and numericals)

After making sure those basics are understood, I think you could get into Sinatra specifics. It would also likely help to have a basic knowledge of html, CSS, and possibly JSON, if you are planning to cover api endpoints.

Darren>> Not much at all to get started. But the more Ruby you know, the better your Sinatra applications will be. As I mentioned in my answer to question 1, Sinatra apps are basically just Ruby apps put on the web, so the standard of your Sinatra app will be directly linked to how much Ruby you know. The Sinatra source code is written in Ruby and a great exercise is to read through it (it’s only around 2000 lines and very well commented). This will help to improve your Ruby skills and help you to understand how Sinatra works.

Nathan>> Sinatra is entirely built using idiomatic ruby constructs and patterns. That said you can start tinkering with Sinatra well before you have a strong grasp on Ruby. I would recommend taking the time to first familiarize yourself with the basic ruby constructs and then you can quickly dive in and learn by doing.

Sudarshan>> Have you seen http://www.sinatrarb.com/ !!:)

I needed a beginner’s level knowledge of Ruby to start using Sinatra.

However, when you are learning a new language like Ruby and using something like Sinatra to do so, it is important to understand where the domain of the framework starts blurring and your problem domain begins to shine.

So you may need to understand Ruby much deeper to efficiently solve the actual problem that you are working on.

Satish>> How important is it for me to know Rack while learning Sinatra?

Andy>> I think Rack is pretty simple for experienced developers to understand, but understanding why it’s needed and how it fits is a bit more abstract and difficult. I don’t think new developers need to understand Rack while learning Sinatra.

Carlo>> Rack is a very basic and important component, don’t be afraid by it. I strongly encourage you to take half a day to study it: it’s time well invested.

Dan>> While Rack is great, I think it could likely be skipped over at the beginning to allow new users to just think in terms of a Sinatra request and response cycle. It might be a good thing to cover a bit further into the lessons, but to help users get to the first goal to dynamically add information to a page, I don’t believe knowledge of Rack is really necessary.

Darren>> I don’t think you need to know much about Rack, except that Sinatra (and most other Ruby frameworks) uses it extensively in the background. Once you get more confident with Sinatra then you can start to dig a bit deeper into how Rack runs the show in the background, but it isn’t essential when you first start.

Nathan>> Rack is the web-server interface that underlies web development in Ruby. Learning about Rack and Rack middleware development will give the developer an insightful look under the hood at how the request/response model of the web interacts in ruby-based web libraries.

Sudarshan>> It is possible to write simple Sinatra apps in the classic style without using Rack. However as your app begins to gather a bit of complexity, you will want to shift to the Modular style. Here’s where Rack will help you organize your app better. This is of course apart from the benefit of web server abstraction Rack inherently provides.

Satish>> While developing Sinatra apps which style (Modular or Classic) should I use?

Andy>> If the audience is beginners, I’d start with classic because you have to keep very little context in your head. As the audience becomes more experienced, move toward a more modular approach.

Carlo>> It depends roughly on your application “size”. Modular style gives you more maintainable and readable code. Anyway start with Classic and then when your app grows up, consider switching to Modular style.

Dan>> I think for teaching beginners, the Classic style is simpler to understand and would be quicker to get students to the learn / reward cycle. Sinatra apps that grow large, is where the modular approach has many advantages. For larger apis, the modular approach is definitely the way to go, but often I find when you are really utilizing that approach it would make sense to just have a full Rails app. I haven’t ended up building out to many very large Sinatra applications so there might be other advice on this point.

Darren>> It doesn’t matter at all. I really like the fact that the Classic style allows you to get started really quickly and build apps with all the code in just a single file. The modular style makes it easier to organize large applications and is a good approach if you are working in teams as each module can be developed independently of the other. Modular apps also make it easier to reuse and share code in other projects.

Nathan>> While tinkering around learning Sinatra or for extremely simple apps, use classic. For writing middle-wares and production applications, I recommend using Modular. Modular is more explicit and I prefer having my Sinatra apps contained within an explicit class of my choosing. I don’t see many benefits to using the classic style.

Sudarshan>> When you are starting to learn Sinatra, I would recommend using the classic style to get a feel of how things work. If you are building a meaningful application of even moderate complexity, the Modular style will help you keep your code organized. Once you are familiar with the Modular style of organizing your Sinatra app, you may be able to work with the Modular style with ease from the word go.

Well, we have set the ball rolling. What’s your take on this? Kindly post your thoughts as comments to this blog post. Looking forward to some interesting read.

Technorati Tags: , , , , , , ,

Posted by Satish Talim

Follow me on Twitter to communicate and stay connected

{ 2 comments… read them below or add one }

Douglas A. August 14, 2013 at 9:00 pm

I have the book. And I’ve done all 4 parts of the lessons on rubysource.
Thanks DAZ! Oh and you too Satish, Victor, Michelle and ashbb.
I wouldn’t know much Ruby without you.

Reply

DAZ August 16, 2013 at 1:56 pm

You’re welcome Douglas! Glad you like the book and tutorials. Keep on rocking with Sinatra!

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: