Learn about Sinatra, a lightweight Ruby web framework, and why it is a useful tool for web developers. Discover ways that Sinatra is similar and different from Ruby on Rails.
- [Instructor] Let's begin by first answering the question what is Sinatra? Sinatra is a simple framework for creating web applications. The word simple here doesn't mean that it's easy to use even though it is. It means that it's bare bones and it does not have a lot of bells and whistles to it. The website for Sinatra is www.sinatrarb.com and yes, it is named for Frank Sinatra, the American singer. Sinatra is written in the Ruby programming language and the code that you're going to write will be written in Ruby too.
You don't need to be an expert in Ruby, but knowing the basics will help you. If you want to learn Ruby, you can search for Ruby Essential Training in the course library to get a good foundation. Sinatra does two things. It routes browser requests to code that can handle those requests and then from there it will render templates back to the browser as a response. Another way to think of it is that Sinatra is the leanest possible version of Ruby on Rails. In fact, that's how it came about.
A group of developers decided to create a new framework using only the most essential parts of the Ruby on Rails framework. Let me show you what I mean. When you interact with the Ruby on Rails framework, the flow works something like this. We start at the browser, it sends a request to the web server, which sends a request through the public folder to the Rails framework and from there we have code that routes our request to the right place and that's typically to a controller. So inside a Rails controller we have more code that handles the request. It can interact with code that's inside our model, which can interact with the database.
That can all be sent back to the controller. It can make multiple trips if it needs and finally when it's all ready, it then triggers a template to render in the view and so it sends the view back to the web server, which sends it back to the browser. The Sinatra framework is very similar, but it strips out all of those nonessential parts. So our browser sends a request to the web server, the web server sends a request to the routing, and then the routing sends a request to the template or the view, which then is rendered back to the web server.
You can see how much simpler this is and for that reason, Sinatra is good for simple, lightweight web applications. Because it's so simplified, it makes it very fast and it has low memory requirements and then you can add on additional features as you need them or you can write your own specialized custom features that meet exactly your needs. I like to think of Sinatra as a Formula One race car. Compare a Formula One car to a regular car. The regular car has many features for user comfort, like the air conditioner, radio, power windows, cruise control and it allocates space for passengers or for carrying packages in the trunk and it may have many safety features, bumpers, door locks, antilock brakes, even a spare tire.
The list is long. A regular car is designed to be an all-purpose vehicle for everyday needs. But that's not the fastest design. If we want to optimize a car for speed, we would throw out all of those extra niceties and keep only the essentials, the engine, the tires, the steering wheel, a seat, and the parts to hold it all together. We don't need an air conditioner in a Formula One car. We don't need a spare tire. A Formula One car has a pit crew. Removing these items saves weight and saves space, which allows the Formula One car to go faster and it allows the body to then be redesigned for speed too and that's the idea behind Sinatra.
There's nothing wrong with all those everyday features. Most people are going to want them. But if you do not need them or if you need to have the fastest possible framework, then a bare bones web framework like Sinatra can help you.
- Installing Sinatra
- Launching the server
- Defining routes
- Using route patterns and parameters
- Redirecting requests
- Rendering dynamic templates
- Using partial templates and layouts
- Defining custom helpers