Ruby Articles - Frameworks

Ruby Frameworks

Ruby is a programming language known for its elegant syntax and focus on productivity. Its web framework ecosystem is diverse, but when it comes to choosing, there’s usually one main option that stands out from the rest.

This ecosystem is primarily divided into two major groups: full-stack frameworks and microframeworks.

Full-Stack Frameworks

These frameworks provide a comprehensive and structured solution for building complex web applications, including all the tools you might need for any web application.

Top Recommendation: Ruby on Rails

Ruby on Rails, or simply Rails, is the most well-known, widely used, and recommended framework for most use cases. Its launch in 2004 popularized Ruby for web development. Rails is based on the “Convention over Configuration” (CoC) principle, which is colloquially known as “magic”, greatly speeding up development.

It uses the Model-View-Controller (MVC) architecture, but its power as a “batteries-included” solution goes far beyond that. In addition to Active Record, its ORM (Object-Relational Mapping), Rails integrates a full suite of tools, such as:

  • Action Mailer for managing and sending emails.
  • Active Job for running background tasks.
  • Action Cable for real-time features using WebSockets.
  • Active Storage for managing file uploads.
  • Action Text for rich text content editing.
  • Propshaft as an asset pipeline.
  • Zeitwerk for code autoloading.
  • Internationalization (i18n) for managing translations.

Rails also includes Hotwire, a modern approach that allows backend developers to implement the frontend. It consists of Turbo, which enables working with the frontend without writing any JavaScript, and Stimulus, a small JavaScript framework for when Turbo isn’t enough.

Rails is ideal for implementing complex web applications like e-commerce platforms or social networks, as it has a mature ecosystem with a vast number of libraries (gems).

Honorable Mentions: Hanami and Padrino

There are other alternatives with different approaches:

  • Hanami: This is a Ruby framework with a focus on modularity. Unlike the monolithic nature of Rails, Hanami is composed of a set of smaller, independent libraries. It’s a great choice for complex applications that benefit from a decoupled architecture or for developers who prefer more control.
  • Padrino: Built on top of Sinatra, its goal is to offer a more structured experience than a microframework without the full weight of Rails. It could be considered a middle ground, ideal for applications that started on Sinatra and need to scale with more integrated tools while maintaining simplicity.

Microframeworks and API Frameworks

Microframeworks focus on providing a minimalist core, giving you complete freedom to build around it.

Top Recommendation: Sinatra

Sinatra is the quintessential microframework in Ruby and the clear recommendation for getting started. It’s an extremely lightweight library that offers a Domain-Specific Language (DSL) for quickly defining routes and actions. It doesn’t impose any project structure, which provides maximum flexibility.

Here is an example of a minimalist but fully functional Sinatra application:

require 'sinatra'
get '/' do
'Hello, world!'
end

Sinatra is ideal for creating RESTful APIs, lightweight microservices, or very small web applications.

Honorable Mentions: Roda and Grape

For more specific needs, there are two other great tools:

  • Roda: A microframework with a primary focus on performance. Its most distinctive feature is its routing tree, which allows for more efficient execution. It’s the option to consider when latency is critical.
  • Grape: A microframework designed exclusively for creating REST-like APIs. It can run on top of Rails or Sinatra. Its specialization allows it to offer a very concise DSL for defining endpoints, managing validations, versioning, and response formats. It’s the perfect tool when the API is the main product.