Overview
Ruby is a dynamic, expressive language with a strong history in web development. Several frameworks have emerged over the years, offering different trade-offs for speed, flexibility, and structure.
In this article, we’ll explore the most popular Ruby web frameworks and help you choose the right one for your project.
1. Ruby on Rails
The most popular and full-featured Ruby framework.
Pros:
- Convention over configuration
- Built-in ORM (ActiveRecord), migrations, mailers, etc.
- Large community and ecosystem
Cons:
- Heavy for small applications
- Learning curve for beginners
Example:
# config/routes.rb
Rails.application.routes.draw do
get '/hello', to: 'welcome#index'
end
# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
def index
render plain: "Hello from Rails!"
end
end
2. Sinatra
A lightweight DSL for quickly creating web applications.
Pros:
- Minimal and fast
- Simple to learn
- Ideal for microservices or small APIs
Cons:
- Manual setup for features like DB access
- Less suitable for large apps
Example:
require 'sinatra'
get '/' do
"Hello from Sinatra!"
end
3. Hanami
A modern and modular Ruby web framework.
Pros:
- Clean architecture
- Modular and lightweight
- Uses ROM for data access
Cons:
- Smaller community than Rails/Sinatra
- Learning curve due to new concepts
Example:
# config/routes.rb
get '/hello', to: 'home#index'
# apps/web/controllers/home/index.rb
module Web::Controllers::Home
class Index
include Web::Action
def call(params)
self.body = 'Hello from Hanami!'
end
end
end
Comparison Table
Framework | Best For | Pros | Cons |
---|---|---|---|
Rails | Full-featured apps | Built-in tools, ecosystem | Heavy, opinionated |
Sinatra | Small apps, APIs | Simple, lightweight | Less structure |
Hanami | Clean architecture | Modular, modern | Smaller community |
Conclusion
- Choose Rails for conventional, full-stack applications.
- Use Sinatra when you need something small and fast.
- Try Hanami if you want modularity and architectural purity.