Understanding Rake in Ruby

Emir Vatric
The Startup
Published in
5 min readJan 25, 2020

--

I am by no means a Rails or Ruby expert, but I was interested in what was happening when we call rake routes/rails routes or rake db:migrate /rails db:migrate, so I decided to read up a little bit about rake and present it here.

What is Rake?

Rake is a tool for organizing tasks, a task runner in Ruby or. Anyone who develops with Rails uses the Rake tool all the time. Rake will run your tests, migrate your database, precompile your assets, and more.

Let’s consider this example. We have a task to wash our hands:

Our task of washing hands consists of more mini-tasks (dependencies) that are building blocks of our main task, so to execute the main task we need to reference and execute our dependencies.

If we wrote this action as a rake file it would look something like this. Rake files we wright in ruby code, every task has a name, every task has an action encoded in do end block, and finally, tasks have dependencies.

Now hopefully it is obvious what is going to happen when we run rake wash_hands, we will start executing our tasks:

$rake wash_hands
Wet your hands
Soap your hands
Wash your hands
Rinse your hands
Dry your hands
Task is done your hands are clean

Because tasks can have similar names, it’s easy to use the same name twice. That’s why Rake has namespaces. For example, we can have a namespace of a dog with tasks.

namespace :dog do
task :feed do
...
end

task :groom do
...
end

task :walk do
...
end
end

To run a namespaced task:

rake dog:feed

Also worth mentioning and providing are some useful Rake options:

  • rake -T (list available tasks)
  • rake -P (list tasks & their dependencies)
  • rake -W (list tasks & where they are defined)
  • rake -V (verbose mode, echo system commands)
  • rake -t (debugging mode)
  • rake -f (use a specific Rakefile)

There is a lot more to Rake but for our purposes, a basic intro into Rake will just do. At the bottom of the document, I linked a great lecture by late Jim Weirich about the subject, please check it out if you are interested in this topic.

Rake in Ruby on Rails

While watching Rails conference keynote on request lifecycle I have heard the expression ‘everything is rake app’ many times or if you have done any development in Rails than you have used Rake tasks. So let’s talk about them, and how can we leverage them.

Worth noting is that since Rails 5 core team decided to have consistency by enabling rails command to support everything that rake does. Some favor using rake over rails.

Don’t confuse Rake with Rack, very similar names, but completely different things.

  • Rake is a task runner.
  • Rack helps Ruby servers & frameworks work together.

Let’s start by running a rake -T or rails -T in a console on our fresh Ruby on Rails project. This command should print the list of available tasks for us to run, and the result will look like this (the list is truncated for space purposes).

Here we can see and recognize some tasks that we run regularly like some under db namespace like db:migrate or db:rollback, but also we notice that some tasks are missing like rake routes. If instead of running rails -T we run rails -P we will notice them showing up.

This happens because our routes task doesn't have a description that tasks above have. This is by design and not an accident.

Ok now we know that this is a rake task but where is it coming from, for this purpose rake gave us command rake -W [task name], let’s try it out.

$rake -W routes
...gems/ruby-2.6.0/gems/railties-6.0.2.1/lib/rails/tasks/routes.rake

When running rake routes the part of code that is being executed is this:

But rake is more useful than just running existing tasks, we can also write our own tasks in rails, those tasks should live in lib/tasks. But also Rake allows us to run multiple tasks in a single line, executing tasks one by one. This can be useful if we want to remove all the data from database, then create it again and run the migrations.

$rake db:drop db:create db:migrate db:seed

Conclusion

When I started writing and reading about this topic I genuinely didn’t pay too much attention to the Rake, it is easy to take it for granted, but it makes our lives a lot easier.

While I am not a mad scientist, master or senior I find it to be satisfying to write about topics in Rails, and especially about beautiful and simple to use tools like Rake.

--

--