Raking in Ruby

Kenny Yoon
3 min readJul 14, 2021

‘Rake’ is a popular tool for coding/defining “tasks” in ruby that can be run in the command line.

For the past few weeks of my coding bootcamp, I have moved on from learning frontend in Javascript and React.js, to learning the backend of constructing simple servers using Ruby.

The main use of ‘Rake’ so far was to more easily manipulate Sinatra and ActiveRecord. I will go deeper into using Rake in ActiveRecord later on in the blog.

Setting up your environment

gem install rake

Using the following command, simply install Rake. Check your Gemfile to see if Rake was correctly installed.

Now create a file named “Rakefile”. We will be doing all of our coding inside this file.

Defining a task

The string following ‘desc’ is a description of the task that will be defined. The the description will come pretty handy when listing out or searching tasks using the rake -T command.

Defining a task follows this structure:

task :[insert name of task] do
[what the task should execute]
end

So in the example above, the task is named “sentence” and executes puts/prints out the line “This is an example sentence.” Let’s check to see if this works in our terminal.

Nice! This effectively prints out our example sentence.

Name spacing

Namespacing in Rake is a way to efficiently organize task commands by grouping them under ‘namespaces’.

Defining a namespace follows this structure:

namespace :[insert name of namespace] do
[insert however many tasks]
end

In order to call the tasks in your terminal, all you have to do is include the namespace name with a colon before your task name:

rake [name of namespace]:[name of task]

In the example code above, I’ve defined a namespace called “example” that has two tasks: “sentence”, “question”. Let’s test them out.

Rake -T

The rake -T command lists out available rake commands.

You can see that the terminal prints out our “sentence” and “question” tasks under the “example” namespace, along with their descriptions we defined.

Rake presets for ActiveRecord in Sinatra

As I’ve mentioned before, ActiveRecord uses Rake in order for its users to easily navigate through its functionalities.(e.g. ActiveRecord Migrations)

To taste a real-life application of Rake, lets go ahead and install Sinatra and ActiveRecord.

gem install sinatra-activerecord
gem install activerecord

Now, in our Rakefile, let’s import our ActiveRecord Rake tasks for Sinatra. We can do this easily by adding in this line:

require ‘sinatra/activerecord/rake’

Now, if we run rake -T:

We are able to see an entire set of rake tasks under the “db” namespace. These tasks allow you to actively manipulate data tables and seed data into your server (this will start to make sense once you start working with ActiveRecord and Sinatra). We can also see that the tasks we defined under the “example” namespace also show up.

Although I’ve only briefly summarized what Rake can do for you as a task runner, there are many more functionalities of Rake that can make your life much easier. Check out this github documentation on Rake if you’re interested.

--

--