Even though Ruby owes most of its popularity to the Ruby on Rails framework, many other great frameworks and libraries also exist in the community. Because they are not so popular as Rails, you can rarely read about them on blogs or see them being mentioned in the newsletters. This article is an overview of interesting but less hyped solutions that still worth learning and using to build awesomeness with Ruby.

This is the first article from the series of articles showing other less-popular but still interesting libraries built with Ruby.

Sinatra

https://longliveruby.s3-us-west-1.amazonaws.com/sinatra.png

Sinatra is a web framework that uses Domain Specific Language (DSL) for quickly creating applications in Ruby with minimal effort.

  • each route is an HTTP method paired with a URL-matching pattern. To define a route you can use a block with optional or mandatory parameters, conditions, or regular expressions
  • it is possible to serve static files that are placed in ./public directory. The public name directory is not included in the URL
  • there are many templates types available to use including HAML, Erb, Nokogiri, SASS, LESS, and many more. Each template language is exposed via its own rendering method
  • it is possible to define helper methods that are later available in routes definitions and templates
  • caching, redirections, session management, sending files and attachment and request manipulation are available and easy to use

Installation

gem install sinatra

A quick launch

Put the following code into the sinatra_app.rb file:

# sinatra_app.rb
require 'sinatra'

get '/' do
  'Hello world from Long live Ruby!'
end

and run the file with Ruby in a standard way:

ruby sinatra_app.rb

the app is now available http://localhost:4567

Documentation

Hanami

https://longliveruby.s3-us-west-1.amazonaws.com/hanami.png

Hanami is a modern web framework that is lightweight, full-featured by the default. It allows us to create web applications simply and productively. The framework is also optimized for speed.

  • Hanami::Router is used for routing which is a Rack compatible, lightweight, and fast HTTP router for Ruby
  • endpoint that handles incoming HTTP requests is an object while the controller is a Ruby module that groups actions
  • by the default, it supports over 20 template engines to render views the way you want
  • it allows managing database schema via Ruby with migrations that come with command-line facilities to speed up the development
  • the framework supports validations, mailers, and models

Installation

gem install hanami
hanami new longliveruby

A quick launch

The hanami new command will generate a skeleton for our app. The next step is to move into the app's directory and run bundle install to install all dependencies. Now we can run bundle exec hanami server to start the server under https://localhost:2300

We can generate a new controller action along with the view that will be rendered after accessing the root URL of our page:

bundle exec hanami generate action web 'home#index' --url=/

Update the view by editing the file /apps/web/templates/home/index.html.erb and putting there some text that will be displayed as a welcome message.

Documentation

Follow to get a free ebook about Rails!

Padrino

https://longliveruby.s3-us-west-1.amazonaws.com/padrino.svg

Padrino is a web framework that was created to make it fun and easy to code more advanced web applications while still adhering to the spirit that makes the Sinatra web framework great.

  • admin dashboard is available out of the box and it includes authentication, templates, scaffold, and internalization. The dashboard can be generated using command-line utilities
  • the framework is solutions-agnostic which means you can select your favorite testing framework, ORM/ODM, rendering, and mocking solution. You can create a customized project by passing arguments to the project creation command
  • generators for models, controllers, mailers, migrations, plugins, and components are available out of the box
  • the framework itself is very easy to learn and highly extensible which make the usage a pleasure and interesting development journey
  • padrino is a very fast solution thanks to its lightweight and slim code stack

Installation

gem install padrino
padrino g project longliveruby -t rspec -e haml -c scss -s jquery -d sequel -b

The parameters passed to the padrino g project command will generate the project with the following specification:

  • ActiveRecord ORM
  • Slim templating language
  • RSpec testing framework
  • jQuery JavaScript library

A quick launch

The padrino g project command will generate a project skeleton along with the solutions that we passed as extra arguments. Let's generate a simple welcome message. Open app/app.rb file and put there the following contents:

module Longliveruby
  class App < Padrino::Application
    register ScssInitializer
    register Padrino::Mailer
    register Padrino::Helpers
    enable :sessions

    get "/" do
      'Hello World from Long Live Ruby!'
    end
  end
end

run the server by executing padrino s command and you can now access the website under localhost:3000 address

Documentation

Grape

https://longliveruby.s3-us-west-1.amazonaws.com/grape.png

Grape is a REST-like API framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop RESTful APIs. It has built-in support for common conventions, including multiple formats, subdomain/prefix restriction, content negotiation, versioning, and much more.

  • you can mount Grape code alongside Ruby on Rails, Sinatra, or any other Ruby web framework code
  • it allows for easy and simple versioning of your API by passing version number as a header, param, or part of the path
  • parameters manipulation is built-in and includes validation and coercion. Over 10 types of parameters are supported out of the box
  • support I18n for parameter-related error messages, but will fall back to English if translations for the default locale have not been provided
  • support for different API formats, authentication, middlewares and logging

Installation

gem install grape

A quick launch

Let's build a simple API on a Rack server that will expose api/welcome endpoint and render the welcome message. Create config.ru file with the following contents:

require 'grape'

class API < Grape::API
  version 'v1', using: :header, vendor: 'test'
  format :json
  prefix :api

  desc 'Return a public message'
  get :welcome do
    { 'message' => 'Hello world from Long live Ruby!' }
  end
end

API.compile!
run API

Run rackup and now you can access the endpoint by visiting the following URL: http://127.94.0.1:9292/api/welcome

Documentation:

Cuba

https://longliveruby.s3-us-west-1.amazonaws.com/cuba.png

Cuba is a microframework for web development originally inspired by Rum, a tiny but powerful mapper for Rack applications. It integrates many templates via Tilt, and testing via Cutest and Capybara.

  • when building a web application, you need to include a security layer. Cuba ships with the Cuba::Safe plugin, which applies several security-related headers to prevent attacks like clickjacking and cross-site scripting, among others.
  • you can mount a Cuba app, along with middlewares, inside another Cuba app
  • given that Cuba is essentially Rack, it is very easy to test with Rack::Test, Webrat, or Capybara. Cuba's own tests are written with a combination of Cutest and Rack::Test

Installation

gem install cuba

A quick launch

Create config.ru file and put there the following code:

# cat config.ru
require "cuba"
require "cuba/safe"

Cuba.plugin Cuba::Safe

Cuba.define do
  on get do
    on "hello" do
      res.write "Hello world from Long live Ruby!"
    end

    on root do
      res.redirect "/hello"
    end
  end
end

run Cuba

After running rackup you can now visit two endpoints:

  • http://127.94.0.2:9292/ - you will be redirected to the /hello path
  • http://127.94.0.2:9292/hello - the welcome message will be rendered

Documentation:

Roda

https://longliveruby.s3-us-west-1.amazonaws.com/roda.svg

Roda is a routing tree web toolkit, designed for building fast and maintainable web applications in ruby. Apps built with Roda are designed to be frozen in the production which eliminates possible thread safety issues.

  • has low per-request overhead, and the use of a routing tree and intelligent caching of internal data structures makes it significantly faster than other popular Ruby web frameworks
  • is built completely out of plugins, which makes it very extensible. You can override any part of Roda and call super to get the default behavior.
  • supports and encourages immutability. Additionally, Roda limits the instance variables, constants, and methods that it uses, so that they do not conflict with the ones you use for your application.
  • is designed to be simple, both internally and externally. It uses a routing tree to enable you to write simpler and DRYer code.

Installation

gem install roda

A quick launch

Create config.ru file and put there the following code:

# cat config.ru
require "roda"

class App < Roda
  route do |r|
    # GET / request
    r.root do
      r.redirect "/hello"
    end

    # /hello branch
    r.on "hello" do
      # Set variable for all routes in /hello branch
      @greeting = 'Hello'

      # GET /hello/world request
      r.get "world" do
        "#{@greeting} world!"
      end

      # /hello request
      r.is do
        # GET /hello request
        r.get do
          "#{@greeting}!"
        end
      end
    end
  end
end

run App.freeze.app

You can now visit three endpoints:

  • http://127.94.0.2:9292/ and you will be redirected to the /hello path
  • http://127.94.0.2:9292/hello and you will see Hello! text
  • http://127.94.0.2:9292/hello/world and you will see Hello world!

Documentation:

Scorched

https://longliveruby.s3-us-west-1.amazonaws.com/scorched.png

Scorched is a generic, unopinionated, DRY, light-weight web framework for Ruby. It provides a generic yet powerful set of constructs for processing HTTP requests, with which websites and applications of almost any scale can be built.

  • it aims to be raw and transparent. Magic has no place. Thoughtful and simple design means there's no requirement for magic. Because of that, most developers should be able to master Scorched in an evening.
  • part of what keeps Scorched lightweight is that unlike other lightweight web frameworks that attempt to hide Rack in the background, Scorched makes no such attempt, very rarely providing functionality that overlaps with what's already provided by Rack. Familiarity with Rack is somewhat of a pre-requisite to mastering Scorched.
  • it has a relatively simple design philosophy. The main objective is to keep Scorched lean and generic. Scorched refrains from expressing any opinion about how you should design and structure your application. The general idea is to give developers the constructs to quickly put together small, medium, and even large websites and applications.
  • it supports sub-controllers to any arbitrary depth, with each controller's configuration, filters, route conditions, etc. applied along the way. This can help in many areas of web development, including security, restful interfaces, and interchangeable content types.

Installation

gem install scorched

A quick launch

Create config.ru file and put there the following code:

# cat config.ru
require 'scorched'

class App < Scorched::Controller
  # To something that gets the muscle's flexing
  route '/articles/:title', method: ['GET'] do
    captures[:title]
  end
end

run App

You can now run rackup and visit http://127.94.0.2:9292/articles/long-live-ruby to see a slug rendered in the browser

Documentation:

Rack App

Rack App is a rack-based micro-framework that is totally addition free. The idea behind is simple. Keep the dependencies and everything as little as possible, while able to write pure rack apps, that will do nothing more than what you defined.

  • The framework is production-ready as it's already powering some backend applications on Heroku
  • It's easy to learn, modular, and with only one dependency - Rack
  • It supports streaming, static file serving, file upload, and params validation

Installation

gem install rack-app 

A quick launch

Create config.ru file and put there the following code:

# config.ru
require 'rack/app'

class SimpleApp < Rack::App
  get '/' do
    "Hello World from Long live Ruby!"
  end
end

run SimpleApp

You can now run rackup and visit the simple website under http://127.94.0.1:9292/

Documentation:

Other frameworks

There are also other Ruby frameworks that you might want to check as well. They are used by fewer developers or not actively maintained.

Download free ebook

Do you know how to build your own RSpec? Level up your skills with the free ebook and create your own testing framework in one evening.

Join Long live Ruby community and get access to the ebook for free.

Download ebook