Rake, an introduction
What is Rake
Rake is a popular Ruby task runner. It is used in Ruby for automating certain jobs. We call these automated jobs ‘tasks’. Tasks can be created to do things like execute SQL commands, print a statement to the console, and even create a file using rake.
Rake was developed by Jim Weirich as the task management tool for Ruby {this is was kind of a nod to ‘MakeFile’s used in the C programming community write all recurring system maintenance tasks in a file, gather all of the source files and make an executable file out of all that}.
Rake is a tool that is used mostly in Rails and requires some idea of how to use ORMs, like Active Record. However, I will not be going into any of those prerequisites or peripherals, instead opting to give more of a bird’s eye view of a this tool that I have developed insane levels of respect for.
Additionally, for a more in depth view of how to use rake or any ruby tools, be sure to visit the ruby guides.
Why should you use it?
Prior to the advent of rake tasks, developers had to either devote a portion of their Ruby program to running tasks like creating new class objects or starting a server or write their own standalone bash scripts! If that last option grabs you, feel free to check this out. For my part, keeping things in Ruby language just makes alot more sense. Below, we’ll discuss how to use Rake and write rake files.
How to use Rake
First we should clearly understand what a ‘task’ is. Think of things like, tracking changes to a program, backing up something to a database, even creating or reporting data. As developers and programmers, we know there are many of these simple jobs that NEED to be done, but here we are CODING, instead!
There but for Rake, these ‘tasks’ would exist as scattered commands in our Ruby code, appearing here and there, driving readability down to near your shins. We don’t want that. For instance, say we wanted to create a new file. We could use shell scripting in bash$ touch new_file…—fun!
OR we can use the tool like so rake db:create_migration NAME="new_file".
Now, I know the above looks like more work, but let’s discuss what this actually represents. Within the above rake command is a task that both creates the aforementioned file, but also puts it into order of execution so that we can track changes that we might want to make. Thus no ‘…’ after the second code.
It’s all fun and good watching me use the tools that I have already become familiar with, but I know you want to know how to build a rake task for yourself. Easy as peaches! The overall convention of writing your own Rake task is:
desc ‘…’
task :name do
# task code …
end
For clarity, there are three main parts, as seen above:
desc Description-gives a brief description of what the task does. This is useful when the list of tasks starts getting large.
task :name` Name-this is the “unique” identifier, with which you will call or execute your tasks
#task code` Code block- this is what your code will do when you have called it.
Going a bit deeper, we can go on to `namespace` the Rake tasks we create, bundling many that share a certain attribute. This is not unlike creating a class is normal Ruby. More about this in a short while.
namespace :seed do
desc “…”
task :create_files => :environment do
#create some files code
endend
For now, just bear in mind that simplifying execution of tasks to something like creating instance methods is the very point of using Rake. This is because at the heart of it all, rake is just a means by which you can execute ruby scripts. Anything you can imagine wanting to do with ruby for increased productivity, can be done with rake.
You can write all the Rake tasks you need to run into a single file titled Rakefile to store all your task handles in one easy to access spot.
Moreover, Rake includes the FileUtils module. This allows us to copy files, create directories, even change file permissions.
Once we define a ‘rake task’, it can be executed from the command line
Here’s a list of useful built in Rake options:
· rake –T(list available tasks)
· rake –P (list tasks & their dependencies)
Get Kyrian Onwe’s stories in your inbox
Join Medium for free to get updates from this writer.
· 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)
In order to run your newly created task, in the terminal, type in rake namespace_of_task_if_any:name_of_task and Bob’s your uncle!
Possible pitfalls and ways to work around them
First we should never confuse Rake with Rack. This is a very simple error, but one I made far too often to not include here. Beyond that, tasks can have similar names. Thus it is easy to use the same name twice for a rake task. This is why Rake has namespacing, for this issue. As an example, you can create a backup namespace for all your backup tasks:
…
namespace :some_group_name
desc “This is why I made this”
task :name
#task code
…
In this way, within each namespaced grouping, we can store tasks, like creating instance methods. Neat!
Another common issue with rake is you can run a rake task and get an output like this:
rake aborted!
Gem::LoadError: You have already activated rake 10.4.2,
but your Gemfile requires rake 10.4.0.
Prepending `bundle exec` to your command may solve this.–this is super common. There is a quick fix for this: use bundle exec rake… I get it, that was more than the 4 letters we were JUST talking about, but for the error above, you just GOTTA roll with it. Apparently this is a huge thing with some coders, so check this out before you shoot the messenger. Don’t shoot the messenger!
We might need to elaborate a dependency for our task to work with. Consider the following code:
namespace :db do
desc 'migrate changes to your database'
task migrate: :environment do
Student.create_table
end
endThe line task migrate: :enviornment do...end points out this ‘dependency’. This line of code tells Rake that it needs to run the environment task before it can run migrate. At this point we should note that our Student.create_table code needs access to the config/environment.rb file because that's where the student class and database are stored. Before we can migrate, we need to give our task access to this file, and the environment task is what will do this for us.
Finally, it is important to remember that when naming your file, though it is built within ruby, the extension should not be .rb, but .rake.
Conclusion
in our lives as developers, we will run into menial tasks that while they aren’t sexy, they are absolutely necessary for the proper writing and functioning for our code. That being said, if you find yourself constantly and repetitively running code to accomplish these menial tasks, consider creating your own custom Rake tasks to keep your cod readable and keep your coding fun.
References
https://linuxhint.com/30_bash_script_examples/
https://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
https://www.rubyguides.com/2019/02/ruby-rake/
https://www.rubyguides.com/2018/09/rack-middleware/
https://learning.flatironschool.com/courses/4332/assignments/135319







