What is RAKE?

Joe McPartland
3 min readAug 2, 2022

Rake is a program written in Ruby that is used to execute tasks. It is based on Unix’s “MAKE” tool and was written by Jim Weirich. Rake tasks are written in a file called Rakefile and is located at the root of a ruby project. The tasks that are defined in the Rakefile can be executed from the command line.

If you’ve worked in Ruby on Rails or Sinatra, you probably already used some Rake tasks such as:

rake console
rake db:seed
rake db:migrate

When using Ruby on Rails, you would type rails console or rails db:migrate. Either way you are using tasks from the Rakefile.

Rake allows us to execute tasks in parallel and gives us the ability to specify prerequisites or dependencies. Rake also includes a library of prepackaged tasks. Type rake -T to see a list of all available tasks.

What are Rake tasks?

Rake tasks are snippets of Ruby code used to execute commands or tasks for administrative purposes.

Rake tasks are written in standard Ruby. Since Rake is a Domain specific Language (DSL), there are specific syntax you need to use when creating them. Tasks contain a name (symbol or string), a list of dependencies or prerequisites (also symbols or strings) and a list of actions.

Some Rake basics

Before we get into building Rake tasks let’s go through some basics. Installing Rake is like any other gem installation, just run:

gem install rake

When you install the Ruby on Rails or Sinatra gems, Rake is already included in the Gemfile.

To find out what version of Rake you have installed or if you need command line help type the following commands:

rake --version
rake --help

For a list of all rake commands, see https://github.com/ruby/rake/blob/master/doc/command_line_usage.rdoc

Creating Tasks

Let’s create a simple task and explain each line of code:

task default: %w[console]desc "Start the console"
task :console do

The first line is the default task. This is task that will run if you do not specify a task. The %w[console]is saying that the task default is dependent on the console task. This means that if you type rake in the command line, the console task will run. This is because we didn’t specify a task, so the default task runs and since the default task has a dependency on the console task, the console task will run. Dependencies can be written several different ways. For example:

task name: [:prereq1, :prereq2]
task 'name' => %w[prereq1 prereq2]
task name: %w[prereq1 prereq2]

Each of these are all legitimate Ruby code. Dependencies or prerequisites are written as Ruby hashes. So whether you use name as a symbol or ‘name’ as a string, it accomplishes the same goal.

Now, the purpose of theconsole task is to run Pry.start. As you can see, first you need to use the keyword task followed by the name of the task preceded with a colon (:). In between the do-end pairs, you write what you want to task to do. Above the task, you can optionally use the desc keyword followed by a string in quotes to show the description of the task. Typing rake -T on the command line will show you all the available task with a description (if a desc was provided).