Rack & Rake

Damely Tineo
3 min readJan 8, 2021
A Rakes Rack

Rack and Rake, although similar in pronunciation and spelling, are different Rails technologies.

Ever heard the term “Rails rides on top of Rack”?

What is RACK?

“Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.” — rubygems.org

Rack is a Ruby gem that acts as a base web server or interface to allow different Rack-compatible Rails frameworks and Rack-compatible Rails web servers to interact interchangeably. As the underlying technology behind some of the most popular Ruby web frameworks, it’s an important technology for us to understand.

Rails request life cycle

As can be expected, Rack is part of every web request and response in our Rails applications including: logging, sessions, caching, security, etc…

How exactly does Rack work? Rack responds to a call method that accepts one argument (env or environment) in which all of the data about the request (paths, HTTP verb, headers, etc) is bundled. After forwarding this to our app server, it then responds with an array of elements for our web server to interpret : status (HTTP status code), headers, and body (content of the response) like so:

env = {
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/hi',
'HTTP_HOST' => '
http://localhost:9000',
}
...def call(env)
[200, {"Content-Type" => "text/plain"}, "Hi there!"]
end

To learn more visit: https://rack.github.io/

What is RAKE?

Rake, on the other hand, is a Rails task runner used to run tests, create backups, execute SQL, seed data, and much more.

Before Rake, developers would have to either write scripts in BASH, a Unix shell and command line language, or make a segment of their program responsible for executing these commands/tasks. Exactly which ones was up to the developer — a practice which oftentimes lead to inconsistency among the code. Then along came Rake with a standard, conventional way of running tasks.

Defining and Using Rake:

Note: Rake is already available to us as a part of Ruby.

  1. Create a file in your top level directory where you will be defining your tasks — Rakefile.
  2. Inside of Rakefile define your task:
task :greet do
puts 'Hi there!'
end

3. In your terminal run: rake + name of your task (ex: rake greet → ‘Hi there!’)

4. That’s it!

Namespaces

Rake also offers Namespaces. Namespaces allow users to easily group or organize group tasks. Below, the Namespace class looks up task names within the (greeting) scope defined by a namespace command:

desc "Greet the world!"
task :greeting do
puts "Hello World!"
end

namespace :greeting do
desc "Greet the world in Spanish!"
task :spanish do
puts "Hola Mundo!"
end

desc "Greet the world in French!"
task :french do
puts "Bonjour le monde!"
end

desc "Greet the world in Portuguese"
task :portuguese do
puts "Olá Mundo!"
end
end
Allowing us to do this:rake greeting
Hello World!

rake greeting:spanish
Hola Mundo!
rake greeting:french
Bonjour le monde!
***Note: As of Rails 5 rake and rails can be used interchangeably so we can now say rails greeting:spanish OR rake greeting:spanish.

Popular rake commands include:

  • rake db:migrate — populates database table
  • rake db:seed — seeds data from seeds.rb file into the database
  • rake/rails routes — prints list of routes available
  • rake/rails console / rails c— allows us to interact with our application from the command line

Hint: For a list of all available tasks within your Rails application run rake --tasks/rake -T from your command line. To learn more about these tasks visit: https://guides.rubyonrails.org/v4.2/command_line.html#rake.

While similar sounding, Rails Rake and Rack are different technologies. Rack interprets and relays information to and from Rack-compatible servers, and Rake assists in the implementation of tasks in our Rails applications.

Thank you for reading!

--

--