Rake is a popular task runner in Ruby.
What is a task?
These are small tasks that without Rake would be scattered all over your project on different files.
Rake centralizes access to your tasks.
Rake also makes a few things easier, like finding files that match a certain pattern & that have been modified recently.
One more thing:
Don’t confuse Rake with Rack, very similar names, but completely different things.
Let’s explore Rake in more detail!
If you have done anything with Rails at all, you’re probably familiar with the
rake db:migrate command.
That’s Rake in action right there.
Notice that Rails, since version 5.0, allows you to call most
rake commands with
In other words:
You can do
rails db:migrate, but Rake is still doing the work.
Here’s a simple Rake task:
desc "Print reminder about eating more fruit." task :apple do puts "Eat more apples!" end
You can put this code inside a file named
Rakefile, or if you’re using Rails, you can save this under
To run this task:
rake apple # "Eat more apples!"
Inside the task, you can write normal Ruby code, but there are some helpful Rake methods you can use.
Rake includes the
That means you can copy files with
cp, create directories with
mkdir_p, and even change file permissions with
Here’s an example:
task :clean_cache do rm_r FileList["tmp/cache/*"] end
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 (covered later in this article) & raise an exception if you don’t want to continue.
Because tasks can have similar names, it’s easy to use the same name twice.
That’s why Rake has namespaces.
You can create a
backup namespace for all your backup tasks.
namespace :backup do task :create do # ... end task :list do # ... end task :restore do # ... end end
To run a namespaced task:
Rake allows you to define a list of other tasks that must run before the current task.
With this, you can do any setup that the task needs.
task create_examples: "load_database" do # ... end
In this example,
load_database will run before
The list of dependent tasks can also be an array of strings or an array of symbols.
Rules define file extension transformations.
task compress: FileList["/tmp/*.txt"].ext(".txt.gz") rule '.txt.gz' => '.txt' do |t| sh "gzip", "-k", "-f", t.source end
The benefit of using rules is that once a file has been compressed, it won’t be compressed again until the source file changes.
Notice a few things in this code:
FileListclass, which is part of Rake, to define a list of files we want to work with.
.txt.gz => .txtdoesn’t mean we go from
txt, it’s the other way around. The arrow is hash syntax.
Here’s a list of useful Rake options:
For example, in a Rails app:
> rake -T test rake test # Runs all tests in test folder except system ones rake test:db # Run tests quickly, but also reset db rake test:system # Run system tests only
You have learned about Rake, a popular task runner for Ruby.
rake -T to find out what tasks are available, create your own tasks & add them to the
Rakefile, or inside the
lib/tasks folder, and remember that
Rack are different things.
Don’t forget to share this article so more people can enjoy it 🙂
Thanks for reading!