Back to Resources

Blog

Posted February 15, 2023

Best Ruby Frameworks for Web Development

Ruby boasts many well-known frameworks for web development, which may reduce your time spent implementing and debugging web apps. This article compares some of the most popular Ruby frameworks.

quote

Ruby is a powerful object-oriented programming language with an emphasis on programmer productivity and simplicity. Ruby is a popular choice in web development due to its simplicity and helpful features.

In this article, you'll look at some of the best web development frameworks that Ruby offers. The frameworks are compared in terms of their features, performance, ease of use, time on the market, and popularity.

1. Ruby on Rails

Ruby on Rails is not only one of the most popular Ruby frameworks for web development, it's also one of the most well-known web development frameworks regardless of language. Initially released in 2005, it's the oldest framework in this list and one of the oldest web development frameworks in use. Since its inception, it has influenced many web development frameworks, such as Python's Django and PHP's Laravel.

Rails uses the model-view-controller (MVC) pattern for web development. Models are Ruby files that map to database tables. Views are, by default, ERB files that are rendered and converted to HTML and sent as a response. Controllers are the components responsible for handling an incoming request, rendering an appropriate view, or responding with data.

Ruby's creator, David Heinemeier Hansson, describes Rails as omakase. It's opinionated: different components are handpicked by the Rails team based on their idea of what makes a great full-stack framework. As such, Rails follows "convention over configuration." The tools are prepared for you, and you don't get as much freedom in mixing and matching different components as you'd get with any other framework.

But the result is a framework that "just works" out of the box. Rails comes with a plethora of features to make a complete full-stack web application, such as the ability to schedule background jobs, send and receive emails, store and edit rich text, send notifications, use WebSockets, and many more.

Rails ships with ERB as the templating engine by default, but it can be switched to something else like HAML or Liquid. Rails also provides a super powerful CLI, which can quickly create, modify, or delete different components such as controllers, models, or views.

The latest version of Rails comes with Hotwire, a revolutionary technique of sending HTML through WebSockets instead of JavaScript that removes the need for Webpack and gives the application a boost in terms of speed and build time.

Thanks to the powerful architecture of Rails, it's straightforward to write gems that can add new features to a Rails app. For example, installing the Active Admin gem gives you a full-featured admin panel for free, or installing the Kaminari gem extends your models with pagination. Gems made for Rails work out-of-the-box with minimal setup. Even tools like Selenium, which don't have a Rails gem, work seamlessly with Rails as long as they have a Ruby binding. This means you can use Selenium to write automation tests for your Ruby app and use Sauce Labs to test the app across multiple OSs, browsers, and devices seamlessly.

Rails is actively developed, and new versions and bug fixes are released frequently. With over 52k stars on GitHub, a vast community, and a bunch of big companies (Airbnb, GitHub, Shopify, etc.) using Rails, it's challenging to beat it.

Pros

  • It comes bundled with a ton of features.

  • It's easy and quick to set up and requires minimal intervention.

  • Following its predefined conventions makes it effortless to create even complex apps.

  • It's actively developed and often adopts new features faster than other frameworks.

  • It has a powerful CLI that can be used to control all aspects of the framework.

Cons

  • It can be bloated if you only require some of its features.

  • It can be slow because of the sheer number of features.

  • It has a less flexible configuration, so not doing things "the Rails way" can make it restrictive and difficult to use.

  • Due to a lot of "magic" happening behind the scenes, it can be challenging to debug, and it takes time to get familiar with a new codebase.

When to Use

Rails is almost always a great choice as the "big name" in the Ruby community. Go with Rails unless you have specific requirements that you're sure are difficult to do with it.

2. Sinatra

Sinatra is more of a DSL than a full-fledged framework. Sinatra lets you quickly create small web applications with minimal effort.

Similar to how you define routes in Rails, Sinatra lets you use HTTP verbs to define routes:

1
get '/' do
2
...
3
end
4
5
post '/' do
6
...
7
end
8
9
put '/' do
10
...
11
end
12
13
patch '/' do
14
...
15
end
16
17
delete '/' do
18
...
19
end
20
21
options '/' do
22
...
23
end
24
25
link '/' do
26
...
27
end
28
29
unlink '/' do
30
...
31
end

It's possible to match routes with parameters using a syntax similar to that of Rails:

1
get '/hello/:name' do
2
# matches "GET /hello/foo"
3
# params['name'] is 'foo'
4
"Hello #{params['name']}!"
5
end

It can render a view using a template rendering engine such as ERB, Markdown, Liquid, RDoc, or HAML:

1
get '/' do
2
erb :index # Renders views/index.erb
3
end

Sinatra has been on the market since 2007 and is actively developed. With over 11k stars on GitHub and a vibrant community, it's a robust web development framework.

Pros

  • It's lightweight and fast.

  • It's easy to learn and develop an app with.

Cons

  • It doesn't have as many features as Rails.

When to Use

Although less feature-complete than Rails, Sinatra is a solid, lightweight, and fast choice for quickly prototyping web applications. If your requirements are small enough and you don't need the entire arsenal of components that Rails offers, Sinatra is a great choice.

3. Hanami

Hanami is a Ruby on Rails alternative for building feature-rich, full-stack web apps. Like Rails, it's an MVC framework and supports many features that Rails does, like routing, controllers/actions, models, views, migrations, validations, mailers, and assets.

However, unlike Rails, it's made up of smaller, single-purpose libraries such as Hanami::Router, Hanami::Controller, and Hanami::View, to name a few. The architecture of Hanami is in stark contrast with that of Rails, which is evident from the directory structure:

1
├── Gemfile
2
├── Gemfile.lock
3
├── Rakefile
4
├── apps
5
├── config
6
├── config.ru
7
├── db
8
├── lib
9
├── public
10
└── spec

Hanami has a clear separation between frontend and backend logic. The controllers and views are stored in the apps directory, whereas code related to the business logic, like models, is stored in the libs directory.

Unlike Rails, Hanami divides models into entities and repositories. Entities are Ruby classes that define the model, whereas repositories define the database associations, queries, and other interactions with the database.

In Hanami, the apps directory can be used to hold smaller subapplications. For example, you can have a JSON-driven api subapplication and a web subapplication living side by side. The directory structure of each subapplication feels similar to that of the app directory in Rails

1
apps
2
3
└── web
4
├── application.rb
5
├── assets
6
│ ├── favicon.ico
7
│ ├── images
8
│ ├── javascripts
9
│ └── stylesheets
10
├── config
11
│ └── routes.rb
12
├── controllers
13
│ ├── books
14
│ │ ├── create.rb
15
│ │ ├── index.rb
16
│ │ └── new.rb
17
│ └── home
18
│ └── index.rb
19
├── templates
20
│ ├── application.html.erb
21
│ ├── books
22
│ │ ├── create.html.erb
23
│ │ ├── index.html.erb
24
│ │ └── new.html.erb
25
│ └── home
26
│ └── index.html.erb
27
└── views
28
├── application_layout.rb
29
├── books
30
│ ├── create.rb
31
│ ├── index.rb
32
│ └── new.rb
33
└── home
34
└── index.rb
35

Hanami is a very young framework that was released in 2014 under the name Lotus. It isn't a trendy framework, with only 6k stars on GitHub. Because of the small community, it can be challenging to get support if you run into any issues while building a complex app.

Pros

  • It's feature-rich.

  • It's flexible and easy to customize.

  • It consumes 60 percent less memory than other Ruby frameworks, according to the developers.

  • It has a fast response time, making use of CDNs to boost site speed.

  • It's possible to have multiple related subapplications in a distinct and clear architecture.

Cons

  • It's not as feature-complete as Rails.

  • There's a lack of documentation, with no examples of a complex application.

  • It's less popular.

When to Use

If you want to build a relatively complex app with multiple related subapplications and you don't want to spend time tweaking Rails to match your requirements, Hanami can be a viable option.

4. Grape

Sometimes, you might want to add REST API capabilities to an existing web app—for example, if you have a full-stack Rails web app and you now want to offer a REST API so that you can build an Android app for it.

Grape is a REST-like API framework that can be added to existing web application frameworks such as Rails or Sinatra to complement the existing web app with REST API capabilities. It has built-in support for standard conventions, such as multiple formats, subdomain/prefix restriction, content negotiation, versioning, and more.

A Grape API is provided by subclassing the Grape::API class. In a Rails project, you can drop the API in the app/api directory, and it will automatically be mounted. It's also possible to mount multiple APIs, which could be either different versions of the same API or multiple components for the same API:

1
class Foo::API < Grape::API
2
mount Foo::APIv1
3
mount Foo::APIv2

The clients can reach different API versions based on path, header, Accept-Version header, or URL parameters. The routing follows a similar pattern to that of Rails:

1
get :books do
2
...
3
end
4
5
post :upload do
6
...
7
end

Grape was released in 2010, so it's been around for quite some time. It has almost 10k stars on GitHub and a large community.

Pros

  • It can be added on top of any Rack-based framework like Rails or Sinatra.

  • It's straightforward to integrate with an existing app without interfering with its code.

  • Grape makes it easy to create a REST API with modern features.

Cons

  • It's an opinionated framework, which means it's limited in terms of customization.

  • If you want to add a REST API to a Rails app, it doesn't integrate with the Rails ecosystem as well as Rails::API.

When to Use

Choose Grape if you want to augment your existing web app with a REST API. If you have a Rails app, use the API mode provided by Rails instead, as it integrates better with the Rails ecosystem. For other frameworks, like Sinatra, using Grape is a great option.

5. Cuba

Cuba is a microframework for web development. It's lightweight and offers minimal features. 

Cuba supports multiple templating languages, such as ERB, HAML, and CoffeeScript via Tilt, and it supports Cutest and Capybara for testing. Cuba offers a Cuba::Safe plugin that applies several security-related headers to prevent attacks like clickjacking and cross-site scripting. You can also write custom plugins to extend Cuba's functionalities.

Cuba provides a DSL for defining routes that feels very similar to that of Rails:

1
require "cuba"
2
require "cuba/safe"
3
4
Cuba.use Rack::Session::Cookie, :secret => "__a_very_long_string__"
5
6
Cuba.plugin Cuba::Safe
7
8
Cuba.define do
9
on get do
10
on "hello" do
11
res.write "Hello world!"
12
end
13
14
on root do
15
res.redirect "/hello"
16
end
17
18
end
19
end

Cuba has been around since 2010, but it has a tiny community with only 1.4k stars on GitHub.

Pros

  • It's lightweight and secure.

  • It can be extended with plugins.

Cons

  • It lacks features.

  • It has a small community, and its documentation is lacking.

When to Use

If you want to build a small, secure web application, you can choose Cuba.

6. Padrino

Padrino is a web development framework built using Sinatra. It offers more functionalities while keeping the simplicity of Sinatra.

Padrino offers full support for many popular testing, templating, and database libraries. Similar to Hanami, it supports mounting multiple apps and provides a generator interface like Rails to quickly create applications, models, and controllers. The routing mechanism of Padrino has all the features you can expect from a full-fledged framework, including named routes, named params, regex matching, and before/after filter support.

Padrino provides a built-in mechanism for sending mail, like Action Mailer of Rails. Padrino also ships with an admin interface with authentication and provides a caching mechanism out of the box.

Padrino was created and open sourced in 2010, so it's been on the market for a long time. However, with only 3.3k stars, it doesn't have a big community.

Pros

  • It was built using Sinatra, so it's easy to understand if you know Sinatra.

  • It offers features like mailing, caching, logging, and an admin panel.

Cons

  • It's not widely popular.

When to Use

If you're familiar with Sinatra and want to build complex applications with it, you can use Padrino to make the process faster and easier.

Summing it All Up

Ruby is an elegant, powerful, and easy-to-use language for web development. Although Ruby on Rails is the king of all Ruby web development frameworks in terms of popularity and features, many more options are available. Whether you prefer a complete, feature-rich framework or something minimal and lightweight, there's a Ruby framework that matches your preference. In this article, you looked at some of the best web development frameworks for Ruby.

No matter what framework you use to build your web application, testing it across various browsers, operating systems, and devices helps ensure that it works correctly for all your users.

Published:
Feb 15, 2023
Topics
Share this post
Copy Share Link

Need to test right now? Get started free.

Ship code that behaves exactly as it should, faster.

© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.