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.
Mini-Challenge: A Tiny Rack App
Write a raw Rack app in config.ru that returns different bodies for / and /about . Run with rackup .
Common Errors (and the fix)
- Returning a string from call — Rack needs an array: [status, headers, body] , where body responds to each .
- Body is not enumerable — Wrap it in an array: ["Hello"] , not "Hello" .
- Missing config.ru — rackup looks for a config.ru with a run line.
- Wrong params key type — Sinatra params keys are strings: params["name"] .
- Middleware not calling the inner app — You must call @app.call(env) and return its result.
Pro Tips
- 💡 Peek under Rails: run rails middleware to see the Rack stack a Rails app uses.
- 💡 Rack::Test: a handy gem for testing Rack and Sinatra apps without a real server.
- 💡 Start small: Sinatra is perfect for webhooks, tiny APIs, and quick prototypes.
📋 Quick Reference — Sinatra & Rack
Concept
Ruby Syntax
Rack app
def call(env); ...; end
Rack return
[200, headers, body]
Add middleware
use Logger
Sinatra route
get "/" do ... end
Read a param
params["id"]
Run a Rack app
rackup (config.ru)
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ A Rack app responds to call(env)
- ✅ It returns [status, headers, body]
- ✅ Middleware wraps an app to add behavior
- ✅ Sinatra gives expressive routes and a params hash
- ✅ Rails itself is a Rack application
- ✅ Next lesson: Background Jobs with Active Job & Sidekiq
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.
Continue this course
- Previous: Testing with RSpec
- Next: Background Jobs with Active Job & Sidekiq