Integrating Sinatra Into Ruby To Expedite Application Development

6 Min Read
Shutterstock Licensed Photo

Sinatra is a great web application library. It can be used to streamline development in Ruby and several other programming languages. A number of articles on Dzone have been written on using Sinatra to develop web applications and other solutions, but there didn’t appear to be an article on setting up Sinatra. We decided to provide a quick tutorial on this process.

Setting Up Sinatra for Ruby Development

The first step is to download the latest stable version of Ruby. Some versions of Ruby are not compatible with Sinatra, so you may need to update it. I would like to emphasize that the 64-bit version is functional, but it may require some packages to be compiled manually. Therefore, this version is only recommended for more advanced Ruby developers.

A better option is to setup version 2.6.5. You will follow the same steps as any of the other Windows installations. You will have to add the Ruby executables to the Path to use it in the console.

After Ruby has been downloaded, you will need to begin compiling it. You will accomplish this by looking for the command console and typing the following command:

ruby -v

If the message is successful, you should see a message similar to the following:

Ruby 2.2.1p85 <2015-02-26 revision 49769>

The exact message may differ, depending on the version of Sinatra you are installing and whether it has been recently updated from the recent file source.

As you can see, the installation process is very straightforward. There are other steps that you may need to take to get the most out of the Ruby interface, such as compiling binaries. However, this material is not necessary for adding Sinatra and beyond the scope of this tutorial. You can find more information on other articles here at Dzone.

What is Sinatra?

At this stage, you have fulfilled the first step, which is installing Ruby and RubyGems. The next step is to start working with Sinatra, so let’s see what this component is about.

Sinatra first appeared on the market in 2007. It started to gain more popularity in 2010 and was made available in RubyGems, due to its simplicity and low file size. Sinatra has been used by large companies such as GitHub, Apple and Linkedin for a variety of services and their web infrastructure. It can also be used for a variety of other applications, such as cryptocurrency platforms that need to oversee bitcoin halving.

Most developers refer to Sinatra as a micro-framework, but the team at Sinatra defines it as a specific domain language. In other words, it works with an implementation under a particular domain.

This means that it only focuses on giving us a series of programs written in Ruby with which you can manipulate the web aspect through the HTTP protocol. Since it is not considered a framework, it does not require a folder or directory structure for your application to start working.

This means that you have set up a development web server where you can see the result of your programs, to see what your small program does just visit the localhost:4267 root in your browser and add:

 /welcome

Once you have taken that step, you will have effectively started the Sinatra environment and can use it with Ruby and RubyGems successfully.

The coding process becomes easier over time. Every time you edit your code, you have to restart the server by pressing the CRTL + C keys in the console where you started the program. This step will stop the server and start it again.

As this step is very repetitive, you can use a tool called Sinatra Reloader, which simply detects changes in the program and automatically restarts the server. To install this component, you just need to type the following in the command prompt:

gem install sinatra-contrib

This will give you a lot of tools to use. Now you will need to modify the program by typing the following into the console:

require 'sinatra'require 'sinatra/reloader' if development?get '/welcome' do "Welcome to Sinatra!"end

As you see, you need to add a new line that tells Sinatra to load the reloader if you are in a development environment. You will need to start the application again by typing:

ruby hello.rb

Now every time you make a change to the file, the server will immediately restart and you will be able to confirm your changes without having to do the whole process again manually.

Conclusion

With this you have finished your tutorial, as you see there are more options for working with Ruby than using Rails. Sinatra is a great tool that allows us to get to develop great applications such as a software inventory tool without the need to adapt to other work methodologies other than your own.

Share This Article
Exit mobile version