Automation with Rake

Rake is Ruby's built-in task runner: you define named tasks and their dependencies in a Rakefile, then run them from the command line to automate building, testing, and other repetitive jobs.

Learn Automation with Rake in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll write tasks with dependencies and namespaces, and see exactly how Rake decides what to run and in what order.

What You'll Learn in This Lesson

1️⃣ Anatomy of a Rakefile

A Rakefile (no extension) holds task :name do ... end blocks. The hash form task build: [:compile, :test] declares prerequisites, namespace groups tasks, and desc documents them. Here's a complete one you can save and run:

2️⃣ How Dependency Resolution Works

When you run a task, Rake walks its prerequisite graph depth-first, running each prerequisite before the task itself, and never running the same task twice. The runnable simulation below recreates that algorithm in plain Ruby so you can watch the order emerge.

3️⃣ Namespaces and rake -T

Namespaces prefix task names ( db:migrate ) to keep large projects tidy, and resolution still chains through them. Meanwhile rake -T prints every task that has a desc , neatly aligned — a self-documenting menu. The two simulations below reproduce both.

🎯 Your Turn

The resolver needs to read each task's prerequisites. Replace the ___ with the hash key that holds the dependency list.

Build a resolver for a graph where clean is a prerequisite of both compile and assets — yet must run only once. Run with ruby graph.rb .

📋 Quick Reference — Rake

Practice quiz

What is Rake?

  • A Ruby web framework
  • A testing assertion library
  • Ruby's build tool and task runner
  • A package manager

Answer: Ruby's build tool and task runner. Rake is Ruby's build tool and task runner; its name comes from 'Ruby make'.

What is the conventional filename Rake looks for?

  • Rakefile
  • tasks.rb
  • rake.yml
  • Gemfile

Answer: Rakefile. Rake reads tasks from a file named Rakefile (no extension).

How do you define a basic task?

  • def :name do ... end
  • rake :name { ... }
  • namespace :name do ... end
  • task :name do ... end

Answer: task :name do ... end. A task is declared with task :name do ... end.

How do you declare that build depends on compile and test?

  • task :build, :compile, :test

The hash form task build: [:compile, :test] declares an array of prerequisites.

If two tasks both depend on clean, how many times does clean run per invocation?

  • At most once
  • Once per dependent
  • Twice
  • It depends on order

Answer: At most once. Rake runs each task at most once per invocation, even when shared.

How do you invoke a task defined inside namespace :db as :migrate?

  • rake migrate
  • rake db.migrate
  • rake db:migrate
  • rake :db:migrate

Answer: rake db:migrate. Namespaced tasks are invoked with the prefix, e.g. rake db:migrate.

What does the desc directive do?

  • Deletes a task
  • Attaches a one-line description shown by rake -T
  • Declares a dependency
  • Marks the default task

Answer: Attaches a one-line description shown by rake -T. desc documents the following task; rake -T lists described tasks.

Which command lists all tasks that have a desc?

  • rake -A
  • rake list
  • rake --all
  • rake -T

Answer: rake -T. rake -T prints every described task with its summary.

When does a file task run its action?

  • Always when invoked
  • Only if the target is missing or out of date
  • Only once ever
  • Never automatically

Answer: Only if the target is missing or out of date. A file task runs only if the target doesn't exist or a prerequisite is newer.

What does task default: :build accomplish?

  • Renames build
  • Hides build from rake -T
  • Makes a bare 'rake' run build
  • Disables build

Answer: Makes a bare 'rake' run build. It sets the default task, so running rake with no args runs build.