Here’s the standard definition of Rack that you may have heard.
Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby.
At the very basic level, Rack is a protocol, just like HTTP. Rack provides a layer between the framework (Rails) & the web server (Puma), allowing them to communicate with each other.
The Rack protocol allows you to wrap HTTP requests and responses in simple interfaces. This simplifies the API for web servers and frameworks into a single method call.
This is exactly similar to how your browser talks to the web server using the HTTP protocol. That allows any client (Chrome, Firefox, or even terminal commands) to talk to any backend server written in any language (PHP, .NET, Ruby, etc.) so you can access the website.
When we say an application is Rack-compliant, it means the following:
It has a
call
method that accepts a single argumentenv
, containing all the data about the request, andIt returns an array containing:
The status code, e.g. 200 for success
A hash containing the headers, and
An array containing a single string, which is the response body.
So that's the Rack specification. Now let's explore the Rack gem.