Sinatra & Rack
Rack is the simple interface every Ruby web framework speaks, and Sinatra is a tiny framework built on it — together they reveal exactly how web requests turn into responses in Ruby.
Learn Sinatra & Rack in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll write a bare Rack app, stack middleware, build routes with Sinatra, read params, and see why Rails itself is just a Rack application.
What You'll Learn in This Lesson
1️⃣ The Rack Interface and Middleware
A Rack app is any object with a call(env) method that returns [status, headers, body] . That one contract is all a server needs to serve your app.
Middleware wraps an app to add behavior — logging, auth, headers — by intercepting call .
2️⃣ Building Routes with Sinatra
Sinatra hides Rack's boilerplate behind expressive route blocks like get "/" do ... end . Request data arrives in the params hash, and views render with ERB.
Your turn. Finish the greeting route so it uses the captured :name parameter, then run it.
Write a raw Rack app in config.ru that returns different bodies for / and /about . Run with rackup .
📋 Quick Reference — Sinatra & Rack
Practice quiz
What is Rack?
- a minimal interface between Ruby web servers and frameworks
- a database library
- a templating language
- a Ruby web server
Answer: a minimal interface between Ruby web servers and frameworks. Rack is a standard interface that connects Ruby web servers to web frameworks and applications.
A Rack application must respond to which method?
- handle
- run
- call
- serve
Answer: call. A Rack app is any object that responds to call(env).
What must a Rack app's call(env) return?
- a string of HTML
- a hash of params
- nothing
call must return a three-element array: status code, headers hash, and an iterable body.
In the Rack return array, what is the first element?
- the request method
- the HTTP status code
- the response body
- the headers
Answer: the HTTP status code. The first element is the integer HTTP status code, such as 200.
What is Rack middleware?
- a database migration
- a CSS framework
- a test runner
- a layer that wraps an app to inspect or modify requests and responses
Answer: a layer that wraps an app to inspect or modify requests and responses. Middleware sits between the server and app, wrapping call to process requests and responses.
What is Sinatra?
- a lightweight Ruby web micro-framework
- a CSS toolkit
- a Ruby ORM
- a JavaScript library
Answer: a lightweight Ruby web micro-framework. Sinatra is a small, expressive web micro-framework built on top of Rack.
Which Sinatra code handles GET requests to the root path?
- route("/")
- get "/" do ... end
- path "/"
- when "/"
Answer: get "/" do ... end. get "/" do ... end defines a handler for GET requests to /.
In Sinatra, how do you read a URL query or form value named id?
- request.id
Sinatra exposes request parameters through the params hash, e.g. params["id"].
What is the conventional file that tells Rack how to start an app?
- app.rb
- Rackfile
- server.yml
- config.ru
Answer: config.ru. config.ru is the rackup file that defines and runs the Rack application.
How does Ruby on Rails relate to Rack?
- Rails cannot use Rack
- Rack is built on Rails
- Rails is itself a Rack application
- Rails replaces Rack entirely
Answer: Rails is itself a Rack application. Rails is a Rack app, so it works with any Rack-compatible server and middleware.