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.
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.
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.
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.
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.
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:
1get '/' do2...3end45post '/' do6...7end89put '/' do10...11end1213patch '/' do14...15end1617delete '/' do18...19end2021options '/' do22...23end2425link '/' do26...27end2829unlink '/' do30...31end
It's possible to match routes with parameters using a syntax similar to that of Rails:
1get '/hello/:name' do2# matches "GET /hello/foo"3# params['name'] is 'foo'4"Hello #{params['name']}!"5end
It can render a view using a template rendering engine such as ERB, Markdown, Liquid, RDoc, or HAML:
1get '/' do2erb :index # Renders views/index.erb3end
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.
It's lightweight and fast.
It's easy to learn and develop an app with.
It doesn't have as many features as Rails.
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.
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├── Gemfile2├── Gemfile.lock3├── Rakefile4├── apps5├── config6├── config.ru7├── db8├── lib9├── public10└── 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
1apps23└── web4├── application.rb5├── assets6│ ├── favicon.ico7│ ├── images8│ ├── javascripts9│ └── stylesheets10├── config11│ └── routes.rb12├── controllers13│ ├── books14│ │ ├── create.rb15│ │ ├── index.rb16│ │ └── new.rb17│ └── home18│ └── index.rb19├── templates20│ ├── application.html.erb21│ ├── books22│ │ ├── create.html.erb23│ │ ├── index.html.erb24│ │ └── new.html.erb25│ └── home26│ └── index.html.erb27└── views28├── application_layout.rb29├── books30│ ├── create.rb31│ ├── index.rb32│ └── new.rb33└── home34└── index.rb35
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.
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.
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.
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.
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:
1class Foo::API < Grape::API2mount Foo::APIv13mount 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:
1get :books do2...3end45post :upload do6...7end
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.
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.
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.
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.
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:
1require "cuba"2require "cuba/safe"34Cuba.use Rack::Session::Cookie, :secret => "__a_very_long_string__"56Cuba.plugin Cuba::Safe78Cuba.define do9on get do10on "hello" do11res.write "Hello world!"12end1314on root do15res.redirect "/hello"16end1718end19end
Cuba has been around since 2010, but it has a tiny community with only 1.4k stars on GitHub.
It's lightweight and secure.
It can be extended with plugins.
It lacks features.
It has a small community, and its documentation is lacking.
If you want to build a small, secure web application, you can choose Cuba.
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.
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.
It's not widely popular.
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.
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.