Hello World, Hey World, Hi World: From Rack to Sinatra to Rails

Last week at Flatiron School we learned about web frameworks for Ruby. We started the week with Rack, then moved on to Sinatra and then Rails. All within the week! Three frameworks in a week? Why?

Sinatra and Rails both use Rack to automate many of the operations of developing a web service. The main reason for learning all the frameworks is to understand what happens under the hood. After all you have to crawl, then walk then finally run!

Below I’ll walk through the basic steps in each web framework to render the static page with the text “Hello World!” on it.

Crawling: Rack

First up, let's walk through setting it up with Rack.

1. First, we create something (eg. a class) that will respond to acall method.

2. The call method needs to return an array with three elements:

  • An HTTP Status code, eg 200

Below is what this will look like. We could create it in my_server.rb:

require 'rack'class MyServer 
def call
return [200, {"Content-Type" =>"plain/text"}, ["Hello World!"]]

Then we need to create a rackup file with the server setup code. This needs to go into a .ru file.

run MyServer.new

Run this code by executing, from the command line:

$rackup file_name.ru

Going to the root URL ‘ /’ you will see Hello World! printed out by your web server!

Walking: Sinatra

Next, let's see what Sinara does with Rack to streamline the above process.

Image for post
Image for post
  1. First, we need a config.ru file to detail to Rack the environment requirements and start the application. A file might look like:
require 'sinatra'
run Application

2. config.ru requires a valid Sinatra Controller to run. A Sinatra Controller is a Ruby Class that inherits from Sinatra::Base allowing Rack-compatible interfaces. We then define the HTTP verb and route in a method like follows:

class Application< Sinatra::Base
get'/' do
"Hello, World!"

After starting a local server so that you can test your app in your browser you should see Hello World! printed out by your web server at the root URL ‘/’!

Much simpler… Sinatra knows how to handle the status code and the content type! But let’s keep going!

Running: Rails

Image for post
Image for post

And with that we arrive at Rails! Rails focuses on the MVC framework to make the process even more efficient! Let's start by creating a rails project with rails new hello_world.

  1. We draw the route by opening the config/routes.rb file and adding the following route inside of the draw block:
get '/hello_world', to: 'static#hello_world'

2. We add a new controller for our static page by adding a new file to the application: app/controllers/static_controller.rb. To use the methods built into the Rails controller system, the controller will inherit from the application controller. Then we include the appropriate route as a method:

class StaticController <ApplicationController
def hello_world

3. Rails gives us two options for rendering the view file: explicit and implicit rendering. For implicit rendering Rails automatically looks for the view file with the same name as the controller action. We create hello_world.html.erb in the static views directory and add:

<h1> Hello World! </h1>

Now you can see Hello World! from the route /hello_world!


Though both Sinatra and Rails use Rack, Sinatra is good for understanding how HTTP requests and routing work while Rails is better for automating that work. Rails also standardizes the process of web development with the help of the MVC framework. This makes jumping into other Rails applications easy! Everything has a place and you will know where to look for it! With the repetitive work out of the way Rails allows developers to focus on other bigger problems! And I’m excited to learn more about Rails and see where it will lead!

Written by

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store