Pankaj Bagwan
Pankaj Bagwan
3 min read



This article has been updated in 2016

Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax.

Rake Features:

  • Rakefiles (rake’s version of Makefiles) are completely defined in standard Ruby syntax. No XML files to edit. No quirky Makefile syntax to worry about

  • Users can specify tasks with prerequisites.

  • Rake supports rule patterns to synthesize implicit tasks.

  • Flexible FileLists that act like arrays but know about manipulating file names and paths.

  • A library of prepackaged tasks to make building rakefiles easier. For example, tasks for building tarballs.

  • Supports parallel execution of tasks.

What is a task?

  • A Task for running test suite
  • Building or Backing up Databases
  • Reporting Statistics
  • Anything that needs to be Automated

These tasks; without Rake can be scattered around your project, but Rake can organize them.

Rake is a gem, written in ruby, hence it makes it easier to write code faster and leaner; has all goodies of Ruby

Don’t confuse Rake with Rack, very similar names, but completely different things. Rake is a task runner while Rack helps Ruby servers & frameworks work together.

Who Uses Rake?

Rails uses Rake for task automations. If you have done anything with Rails at all, you’re probably familiar with the rake db:migrate command or rake routes.

Notice that Rails, since version 5.0, allows you to call most rake commands with rails instead of rake, however they still are rake tasks. You can do rails db:migrate, but Rake is still doing the work.

How to Write a Rake Task

Have a look at following example

desc "Run unit tests"
task default: %w[test]
task :test do
  ruby "test/unittest.rb"

A task named “test”, which – upon invocation – will run a unittest.rb file in Ruby.

A task named “default”. This task does nothing by itself, but it has exactly one dependency, namely the “test” task. Invoking the “default” task will cause Rake to invoke the “test” task as well.

You can put this code inside a file named Rakefile, or if you’re using Rails, you can save this under lib/tasks/test.rake.

To run this task:

rake test

What we can do using Rake? Pretty much anything that is to be automated, but here are some popular one

  • Execute a Ruby script
  • Run System Commands (Copy Files, Symlink, Download Files)

Following is an example to delete all files under tmp/ folder

task :clear_tmp do
  rm_r FileList["tmp/*"]

Be careful with system commands, like rm_r (remove with recursion) as it will delete files without confirmation, if you want to add a confirmation step you can add a dependent task & raise an exception if you don’t want to continue.

Namespaces in Rake

Because tasks can have similar names, it’s easy to use the same name twice. Using namespace we can make task name more meaningful at the same time using same name under different namespace. For example, db:clean and tmp:cache:clean. This name clearly suggest what these tasks do; at the same time uses same name for tasks (albeit under different namespace)

namespace :tmp do
  namespace :cache do
    task :clean do
      # ...
    task :purge do
      # ...
    task :restore do
      # ...
    task :backup do
      # ...

To run a namespaced task:

rake tmp:cache:clean

Dependent Tasks

Rake allows you to define a list of other tasks that must run before the current task in order as they are specified. With this, you can do any setup that the task needs.

task migrate: :create do
  # ...

Or we can specify more than one dependencies

task backup: [:create, :migrate] do
  # ...

Run A Rake Task Within Another Task

If instead of having a set of task that run BEFORE the current task, you want to invoke another task within the current task, then you can use the following code.

task :coverage do
  ENV['COVERAGE'] = 'true'

This can be helpful to set environment variables that enable test coverage & other options.

About The Author

I am Pankaj Baagwan, a System Design Architect. A Computer Scientist by heart, process enthusiast, and open source author/contributor/writer. Advocates Karma. Love working with cutting edge, fascinating, open source technologies.

  • To consult Pankaj Bagwan on System Design, Cyber Security and Application Development, SEO and SMO, please reach out at me[at]bagwanpankaj[dot]com

  • For promotion/advertisement of your services and products on this blog, please reach out at me[at]bagwanpankaj[dot]com

Stay tuned <3. Signing off for RAAM