0Pricing
Ruby Academy · Lesson

Sinatra Basics

Lightweight web apps.

Sinatra Basics is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Sinatra?

Sinatra is a lightweight DSL for building web applications in Ruby with minimal effort.

  • No heavy conventions like Rails
  • Define routes directly in a single file
  • Perfect for small apps, APIs, and prototypes

You describe HTTP verbs and paths, and return a response body.

require 'sinatra'

get '/' do
  'Hello from Sinatra!'
end

Installing Sinatra

Sinatra is a gem. You install it with the gem command or via a Gemfile.

  • gem install sinatra
  • Or add gem 'sinatra' to a Gemfile and run bundle install

Run your app with ruby app.rb and it serves on port 4567 by default.

# Gemfile
source 'https://rubygems.org'
gem 'sinatra'
gem 'puma'

Your First Route

A route pairs an HTTP verb with a URL path and a block. The block's return value becomes the response body.

  • get, post, put, delete map to HTTP methods
  • The last expression in the block is sent to the client
require 'sinatra'

get '/hello' do
  'Hello, world!'
end

get '/bye' do
  'Goodbye!'
end

Returning HTML

Since the return value is just a string, you can return raw HTML directly.

  • Useful for quick pages without a template engine
  • For real apps prefer ERB or HAML views
require 'sinatra'

get '/' do
  '<h1>Welcome</h1><p>This is my Sinatra app.</p>'
end

Modeling a Response in Plain Ruby

Before wiring HTTP, you can model what a handler returns using a plain method. This is the logic a route would call.

  • Keep business logic out of route blocks
  • Makes code testable without a server
def greeting(name)
  "Hello, #{name}!"
end

puts greeting('Sinatra')

HTTP Verbs

Sinatra supports all common HTTP verbs as route methods.

  • get reads data
  • post creates data
  • put/patch update data
  • delete removes data
require 'sinatra'

post '/users' do
  'Creating a user'
end

delete '/users/:id' do
  "Deleting user #{params['id']}"
end

Setting Status Codes

You control the HTTP status code with the status helper.

  • status 404 sets Not Found
  • status 201 signals a created resource
  • Defaults to 200 OK
require 'sinatra'

get '/missing' do
  status 404
  'Not found'
end

Configuration with set

Sinatra is configured with set, enable, and disable.

  • set :port, 8080 changes the listening port
  • set :bind, '0.0.0.0' accepts external connections
  • enable :logging turns on request logs
require 'sinatra'

set :port, 8080
set :bind, '0.0.0.0'

get '/' do
  'Configured app'
end

before Filters

A before filter runs before every matching request. It is ideal for setup like authentication or content headers.

  • Runs in the same context as routes
  • Can read and modify params and response headers
require 'sinatra'

before do
  content_type :json
end

get '/' do
  '{"ok": true}'
end

Modular Apps

For larger projects use the modular style by subclassing Sinatra::Base.

  • Keeps multiple apps isolated
  • Easier to mount under Rack
  • Call run! to start it manually
require 'sinatra/base'

class MyApp < Sinatra::Base
  get '/' do
    'Modular Sinatra'
  end
end

MyApp.run!

Simulating a Router Locally

You can mimic Sinatra's routing logic with a simple hash dispatcher to understand the concept.

  • Keys are paths, values are handler procs
  • Lookup returns the response body
routes = {
  '/' => -> { 'Home' },
  '/about' => -> { 'About us' }
}

puts routes['/'].call
puts routes['/about'].call

Quick Check

Test your understanding of Sinatra basics.

Recap

You learned the essentials of Sinatra:

  • Sinatra is a lightweight web DSL installed as a gem
  • Routes pair an HTTP verb with a path and a block
  • The block's return value is the response body
  • Use status, set, and before filters for control
  • Modular apps subclass Sinatra::Base

Next, you will handle dynamic routes and params.

Frequently asked questions

Is the “Sinatra Basics” lesson free?

Yes — the full text of “Sinatra Basics” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Sinatra Basics”?

Lightweight web apps. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sinatra Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Sinatra Basics
  2. Routes and Params
  3. JSON APIs
  4. Middleware and Rack
← Back to Ruby Academy