window.pipedriveLeadboosterConfig = { base: 'leadbooster-chat.pipedrive.com', companyId: 11580370, playbookUuid: '22236db1-6d50-40c4-b48f-8b11262155be', version: 2, } ;(function () { var w = window if (w.LeadBooster) { console.warn('LeadBooster already exists') } else { w.LeadBooster = { q: [], on: function (n, h) { this.q.push({ t: 'o', n: n, h: h }) }, trigger: function (n) { this.q.push({ t: 't', n: n }) }, } } })() The Role of Rack in The Ruby Ecosystem - The Codest
The Codest
  • About us
  • Services
    • Software Development
      • Frontend Development
      • Backend Development
    • Staff Augmentation
      • Frontend Developers
      • Backend Developers
      • Data Engineers
      • Cloud Engineers
      • QA Engineers
      • Other
    • It Advisory
      • Audit & Consulting
  • Industries
    • Fintech & Banking
    • E-commerce
    • Adtech
    • Healthtech
    • Manufacturing
    • Logistics
    • Automotive
    • IOT
  • Value for
    • CEO
    • CTO
    • Delivery Manager
  • Our team
  • Case Studies
  • Know How
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About us
  • Services
    • Software Development
      • Frontend Development
      • Backend Development
    • Staff Augmentation
      • Frontend Developers
      • Backend Developers
      • Data Engineers
      • Cloud Engineers
      • QA Engineers
      • Other
    • It Advisory
      • Audit & Consulting
  • Value for
    • CEO
    • CTO
    • Delivery Manager
  • Our team
  • Case Studies
  • Know How
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
Back arrow GO BACK
2022-08-25
Software Development

The Role of Rack in The Ruby Ecosystem

Nicolas Nisoria

Learn more about the role of rack in the ecosystem of Ruby from our our expert and up skill your ruby game.

While working with Ruby web frameworks it’s common to take things for granted. We know the framework will handle the HTTP requests and execute the middleware logic for us. As we get more curious we start wondering what is behind the scenes, there’s where we start hearing about Rack.

What is Rack?

The project is described as “A modular Ruby web server interface”. Rack is the interface that let us create web applications unifying the API for web servers, web frameworks, and middleware.

rack ruby scheme

As described in the above picture, Rack acts as a middleman between our Web Application and the Application Server, it wraps the HTTP requests in the simplest
way possible.

Rack Application

A Rack application is a Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns a non-frozen Array of
exactly three values:

  • The status,
  • the headers,
  • and the body.

    You can find the detailed specification of a Rack Application here.

require 'rack'

class RackApp
  def call(env)
        status = 200
        headers = { 'Content-Type' => 'text/html' }
        body = ['<h1>My Rack App<h1>']

    [status, headers, body]
  end
end

Rack::Handler

Handlers connect web servers with Rack. Rack includes Handlers for Thin, WEBrick,FastCGI, CGI, SCGI and LiteSpeed. Each application server that supports Rack should
provide a handler to create the connection (Puma has its own handler).Handlers usually are activated by calling MyHandler.run(myapp). A second optional hash can be passed to include server-specific configuration.

Using Thin application server

Rack::Handler::Thin.run(app)

The default file to add the configuration is config.ru and you can execute itusing rackup command in your console.

Rack Middleware

Rack allows us to create middleware applications (applications between our main web application and the application server). These middleware applications are chained together and executed sequentially.

Rack Middleware must implement all the specifications of a Rack Application and meet the following points:

  • It must be a class,
  • have an initializer that receives only one parameter (the main application),
  • and call the next middleware or the application.
class RackMiddleware
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end

Rack into Practice

Now that we know the basics, we are ready to create our first Rack Application with Rack Middleware and run it using Puma (Application Server).

Install the dependencies

Make sure you have the rack gem and the puma gem installed.

gem install rack
gem install puma

Create the configuration file

First, we have to create a file called config.ru and this file will make use of
the Rack::Builder DSL to run the application and add the middleware.

Add the Rack Application

Within the config.ru file, we will add the simple Rack Application we defined in
the previous sections.

# config.ru

class RackApp
  def call(env)
    status = 200
    headers = { 'Content-Type' => 'text/html' }
    body = ['<h1>My Rack App<h1>']

    [status, headers, body]
  end
end

Add the Rack Middleware

Here we will make a small modification to our simple middleware and now it will add the server software to our HTML body after being executed.

# config.ru

class RackMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)

    body << env['SERVER_SOFTWARE']

    [status, headers, body]
  end
end

Run the Application Server

As a last step, we will run the server and see our application running. Our config.ru file will look as follows:

# config.ru

class RackApp
  def call(env)
    status = 200
    headers = { 'Content-Type' => 'text/html' }
    body = ['<h1>My Rack App<h1>']

    [status, headers, body]
  end
end

class RackMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)

    body << env['SERVER_SOFTWARE']

    [status, headers, body]
  end
end

use RackMiddleware
run RackApp.new

In the last lines, we specify the middleware using use and we run the application using run.We are ready to execute rackup in our console and see the server running. We can check the port where our application is running and we should see something like this after accessing it:

rack server text

Conclusions

Sometimes is good to go back to the basics and learn about the insights into the technology we work with. Learning Rack give us a clear overview of the architecture and reveals the “magic” behind the Ruby Web Frameworks.

cooperation banner

Related articles

Software Development

GraphQL Ruby. What about performance?

GraphQL, like any technology, has its problems, some of them directly result from the architecture and some are identical to what we see in any other application. However, the solutions...

The Codest
Tomasz Szkaradek Development Architect
Software Development

A Simple Ruby Application from Scratch with Active Record

MVC is a design pattern that divides the responsibilities of an application to make it easier to move about. Rails follows this design pattern by convention.

The Codest
Damian Watroba Software Engineer
E-commerce

Is Ruby on Rails a Good Technology to Build an MVP?

A minimum viable product (MVP) is one of the principles of the Lean Startup Methodology. The goal is to help the entrepreneurs start the process of learning as soon as...

Nicolas Nisoria

Subscribe to our knowledge base and stay up to date on the expertise from the IT sector.

    About us

    The Codest – International software development company with tech hubs in Poland.

    United Kingdom - Headquarters

    • Office 303B, 182-184 High Street North E6 2JA
      London, England

    Poland - Local Tech Hubs

    • Fabryczna Office Park, Aleja
      Pokoju 18, 31-564 Kraków
    • Brain Embassy, Konstruktorska
      11, 02-673 Warsaw, Poland

      The Codest

    • Home
    • About us
    • Services
    • Case Studies
    • Know How
    • Careers
    • Dictionary

      Services

    • It Advisory
    • Software Development
    • Backend Development
    • Frontend Development
    • Staff Augmentation
    • Backend Developers
    • Cloud Engineers
    • Data Engineers
    • Other
    • QA Engineers

      Resources

    • Facts and Myths about Cooperating with External Software Development Partner
    • From the USA to Europe: Why do American startups decide to relocate to Europe
    • Tech Offshore Development Hubs Comparison: Tech Offshore Europe (Poland), ASEAN (Philippines), Eurasia (Turkey)
    • What are the top CTOs and CIOs Challenges?
    • The Codest
    • The Codest
    • The Codest
    • Privacy policy
    • Website terms of use

    Copyright © 2025 by The Codest. All rights reserved.

    en_USEnglish
    de_DEGerman sv_SESwedish da_DKDanish nb_NONorwegian fiFinnish fr_FRFrench pl_PLPolish arArabic it_ITItalian jaJapanese ko_KRKorean es_ESSpanish nl_NLDutch etEstonian elGreek en_USEnglish