Building an App Series Part 1: Why Sinatra is an Essential Tool for the Aspiring Ruby Developer
If you are reading this, chances are you have most likely dabbled with a bit of Ruby and you are at least superficially interested in where Ruby takes you. Most programming languages use web frameworks to organize code, generate dynamic information, and provide a certain experience to both developers and an application’s intended users. You have most likely heard of Rails and perhaps even the term “Rails Magic”. Maybe you’ve perused the Rails documentation and you want to learn more. If any of this is true, then, keep reading.
This is such an important question to consider before building your application. It is important not only in the sense that it is the responsible approach to building a web application, but also because the answer to this question will undoubtedly change during various phases of your application’s construction. Knowing that the constructs and intentions of your app will change based any number of factors requires us as developers to understand what tools are at our disposal and how to leverage them in a way that maximizes DRY (Don’t Repeat Yourself) methodologies within our code and minimizes repeating ourselves during the build process.
Rails and Sinatra are the primary frameworks at our disposal. Rails is the more reputable of the two and is more widely used. At Launch Academy, we believe that understanding the simplicity of Sinatra is essential to understanding the complexity of Rails. Learning Sinatra will give you a greater appreciation for what Rails does for you. Additionally, you will come to appreciate Sinatra as a light web framework that can do essential programming tasks, and do them very well. Let’s take a look at both frameworks and discover how and why they are useful in their own ways.
For web applications that do not require an extensive number of views or require a large number of classes, Sinatra gets the job done very efficiently.
Sinatra is incredibly lightweight. When you create a Sinatra application, you are given blank canvas. Developers appreciate Sinatra for its necessity to be explicit in rendering specific views and dispersing pertinent information. It is great for beginners because all of the logic for a Sinatra application can exist in a single file, and the structure is highly configurable. Attempting to navigate a Rails application without the proper introduction can cause serious confusion, especially for a beginner. Sinatra gives entry-level users an ideal utility to create small-scale dynamic applications and solve “beginner problems” that are quite more difficult to resolve in Rails. At Launch Academy, our students learn the essentials of web development through Sinatra. HTTP, template rendering, the params hash, and view helpers are all explored through this lightweight framework. This knowledge translates well when it comes time to learn more complex and mature web frameworks such as Rails.
The image below accurately represents a Rails app configuration vs. a Sinatra app. Both apps have a homepage that simply says “I’ve got the world on a string”, a Rails application occupies 1.7 MB of disk space, where the Sinatra application only takes up 70 bytes.
The image below shows the components of these applications and the files they come with in the directory. Notice all of the files you may not need in the Rails app on the left, where you can execute the same simple program with 4 lines of code.
Like any web framework, there are certain drawbacks to consider when using Sinatra. First, Sinatra does not make any assumptions for you. That can be a good thing on a small-scale project where you know the exact configurations you may want. When trying to build larger scale applications, the lack of these convenient assumptions can cost hours of development time and cause headaches when trying to troubleshoot issues that may arise. Since there is no underlying structure, developers must be explicit, intelligent, and have tremendous foresight as to how to structure and configure the contingencies upon which their dynamic web applications are built.
Rails abides by a MVC (Models, Views, and Controllers) structure. The Model is responsible for the data in your application. Within Rails, it is a layer of code between the Database and the rest of the application. The Controller acts as a traffic cop, deciding what to retrieve from the models, what to save to the models, and which View should be rendered. The Views define the way data from the Models will be displayed. It is the interaction of these three components and the inherent structure provided by the Rails framework that makes Rails so powerful. The inherent structure of Rails allows it to make assumptions pertaining to how your application should be structured, which helps with troubleshooting potential issues and intelligently organizing elements of your code. With the proper understanding of how it works and how you should manipulate it, Rails is a fantastic tool admired and utilized by many Ruby developers all over the world.
Take Your Time. Learn Efficiently.
Through its elegant route definitions and its support for easy-to-understand templating engines such as erb, Sinatra is a great introduction to web application development in Ruby. For aspiring Ruby developers, we advise you to work with Sinatra before jumping into Rails. Rest assured, you will have a greater grasp of framework-oriented concepts; you will also make some mistakes along the way that will enhance your programming skills and shorten the time required to become acclimated with Rails.
During your web development journey, you may decide to learn Rails. It is important for you to understand that Rails is not the end all, be all framework. It is undoubtedly awesome, handy, and convenient. While it is the primary tool for the majority of Ruby developers, it is not the right tool for every job. Keep this in mind going forward in your own personal development, and becoming a Ruby developer will be an enjoyable, rewarding experience. There’s a certain value to approaching software development in Ruby with a predetermined trajectory. Consider Sinatra a useful tool on the way to Rails.
In the coming articles in this series, we will build an entire web application in Sinatra, starting from the inception of an idea and ending with a complete, polished product. But first, try these two quick practice problems to get your feet wet with Sinatra.
- Build a Sinatra application that displays “Hello World!” on the home page.
- Build a Sinatra application that uses Ruby to display the current time. If you refresh the page, the current time will update.
For guidance, check out the Sinatra documentation.
Next time, we will discuss the answers to these two problems, as well as discuss the Product Development Life Cycle.