Try Sinatra before Moving up to Ruby on Rails – InApps is an article under the topic Software Development Many of you are most interested in today !! Today, let’s InApps.net learn Try Sinatra before Moving up to Ruby on Rails – InApps in today’s post !

Read more about Try Sinatra before Moving up to Ruby on Rails – InApps at Wikipedia



You can find content about Try Sinatra before Moving up to Ruby on Rails – InApps from the Wikipedia website

Have you ever wondered if it would be possible to make a functional website from scratch, but without paying the heavy time investment of learning how to program? Never fear! Sinatra has your back. Sinatra is described as a domain-specific language (DSL) “for quickly creating web applications in Ruby with minimal effort.”

So why should you try Sinatra, and what exactly is it? Sinatra is a simple web framework that gives a developer the “bricks” to build dynamic websites. It is driven by the Ruby language and represents a solid step before moving up to its better-known big brother Ruby on Rails.

What’s exciting about Sinatra is how quickly you can write websites that respond to users (i.e. HTTP requests) by outputting useful information (HTML pages on the browser) — all without needing decades of programming experience. There are other technologies out there that can help you quickly put those websites live on the net, like Heroku or Engine Yard.

Ruby is a language loved by developers for its ease of use. It is overseen by a vibrant community, as are the projects that spring from it. Sometimes reading the odd names for things makes you wonder if you have walked into the wrong nightclub by mistake, but don’t worry unduly. Treat this article as an open-top bus tour visiting all the sights at once. So if you are new to any of this, take your time. I only assume that you have basic computing literacy and enjoy bus tours.

The only tools you need to get going are a laptop or a virtual machine that you have admin access to, and a terminal or command prompt. For the purposes of this article, I’ll assume you have a Mac in front of you, with Homebrew installed. But most of the steps work exactly the same way on Windows, once you have downloaded a Ruby installation. Make sure you have a text editor available too.

Read More:   Bring in the Bug Hunters or Maybe Not for Some Open Source Projects – InApps Technology 2022

So let’s start by installing Ruby. After that, we can install the modules (called “gems“) that Ruby needs to build Sinatra. (Yes, there is a kind of “dependency vortex” or bootstrapping going on already — “you first need to have X before running Y”. This happens a lot.)

Type the following into the prompt:

Do you see some version information? Ok. But if you didn’t put it there, you should probably leave this installation of Ruby alone, as your system is probably using it and expects it not to change. So check:

If you do see /usr/bin/ruby, it is the pre-installed macOS system Ruby.

So either way, use Homebrew to install a nice fresh Ruby with:

And Homebrew will start the (lengthy) process of installing (or updating) Ruby. It should tell you to run a command to ensure that your Mac will find the fresh ruby on the PATH. The last bits might be different, but run this and the following “source” command to update the PATH and the shell you are working in. In my case I saw:

Now if you run that version command from above again, it should reflect your fresher Ruby. And running the “which” command again will also confirm that you are running it from elsewhere. You might also want to make yourself a working directory if you didn’t do that already:

Now that we have ruby installed, try the following:

As you can guess, we asked Ruby to run or “execute” a simple program immediately, and it did that. It spat out our welcoming statement using the “puts” command. Normally, we would write programs in a tidy file, of course.

Read More:   The Programming Language’s Biggest Release Yet – InApps 2022

Next, let’s install Sinatra as a Ruby gem. So we’ll ask the installed gem manager program to do that:

If everything is good, you should get back a list of installed gems, including a version of Sinatra and various dependencies. One name you will also see in the list of exotic gems is Rack. This is the gem used by Sinatra to interpret HTTP commands, and make them easy for the developer to use. (If Ruby has problems building native libraries, Mac users may need to install some of Xcode, which is Apple’s developer tool suite. Try “xcode-select —install”)

Now let us also add the gem for a web server called Puma:

Sinatra’s job is to communicate with a web server, and Puma happens to be one it is already acquainted with.

So now surely we can do some web stuff? Oh yes, we can. Open an editor, write the following and then save it as “singing-with-sinatra.rb”. Of course, any name will do, but use the suffix “rb” to indicate a Ruby program:

Now we will run our own local web server, running our rather simple site code and see what we get:

Now, you should get output something like below. Next, we’ll need to do a quick analysis of what this all means:

The dependency vortex is in full effect, so there is quite a lot of information here:

  • The website that Sinatra just spun up, which only exists on our local machine, is accessible at the URL http://127.0.0.1:4567 (to see our welcoming page, add “/hi”). “127.0.0.1” or “localhost” is just the network way of referencing the local machine.
  • That “4567” is the port usually used informally for Sinatra during development.
  • Puma, the web-server, has a version distinct from the fresh Ruby version.
  • The OS recognizes a specific process identifier (PID). Remember that your OS is behind everything, keeping the show on the road.
  • Plus, our shell is not ours any longer. Until we press Ctrl-C, we won’t get it back!
Read More:   oneAPI and Your New Hardware – InApps Technology 2022

While Sinatra is on stage, you should see the welcoming response if you browse to http://localhost:4567/hi, and you will also see what the server is logging in the shell:

Before I ask you to leave the tour bus and explore, let’s just prove that we are actually creating a real web page. In the example below, I add a few extra bits:

  • We now have some very simple HTML tags as well as text.
  • I added an extra route. In Sinatra, a route is an HTTP method paired with a URL-matching pattern (e.g. “/hi”). And each route is associated with a code block.
  • I asked Ruby to interpolate some code into the output. You can see that the code includes the time.

So if you run the new program:

…and you browse to http://localhost:4567/hi you will see:

Sinatra

and if you browse to http://localhost:4567/bye you will see something like:

Sinatra

OK, now it’s time to leave the bus and explore on foot. Some suggestions:

  • Take more control of your gems with bundler. This is what the good developers do.
  • Take more control over your Ruby installation(s) with rvm.
  • With your new knowledge, see how you can deploy your Sinatra site live with Heroku.
  • Or see more lovely Sinatra in my examples here.

I hope you enjoyed the tour!

Photo by Lara Wehbe from Pexels.



Source: InApps.net

Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      Success. Downloading...