BASICS OF RAKE

Melike Kilic
4 min readApr 2, 2020

Recently I’ve learned a pretty valuable Ruby gem in one of the labs on learn.co: Rake. Despite using some Rake commands for a while, it was my first time getting a better grasp of Rake. For those of you who are wondering what this Rake thing is, keep reading!

Rake is a Domain-Specific Language(DSL)*. What this means is that it is precisely for Ruby-related things. Its primary function is to handle administrative commands/ tasks.

In this blog post, we will be walking through:

  1. Rake Tasks
  2. Namespacing
  3. Task Dependencies

* Domain-Specific Languages (DSLs) are languages designed to write programs that solve problems from a specific domain.

1. Rake Tasks

Rake can perform tasks, which you will need to describe in your Rakefile, for your project. Rake tasks are similar to the ‘def’ methods in Ruby with one essential exception: You can run Rake tasks in the terminal while you can run Ruby methods inside of a Ruby file. Since we know a thing or two about Rake tasks now, let’s create one!

To create a Rake task, we will be writing three main things in our Rakefile:

1. ‘desc’ followed by a string that contains the description(optional)

2. ‘task’ followed by the name that identifies the task (as a symbol)

3. the code that we want to execute in a block

our code is ready to be executed!!!

Let’s run it!

We use rake + name of the task to run our task:

Yayyyy!!

You might be wondering what the first line is doing since we don’t see anything about it in the terminal when we run the code. The first line is describing the code in the block. If we run rake -T, we will be able to view the available Rake tasks (we have only one, for now) and their descriptions.

Everything was going pretty well until now. But..what if I wanted to have more Rake tasks about my favorite songs by my favorite singers? Would I need to create those individually?

The answer is yes, and no.

I would create a new task for every singer, but I could use a concept that Rake supports: Namespacing!!

2. Namespacing

Namespacing is a way to organize your code by grouping tasks. In our case, I can group the below tasks by using a namespace.

Our command is going to be rake + name of the namespace + name of the task that we want.

Here I ran both tasks to show you the output:

Now that we know how to group our tasks, things are a lot neater.

Despite being in love with these two singers, I don’t want to listen to the same songs all the time. No matter how much you love them, the same songs suck after a while, you know..

How I wish that Chet Baker were alive. I am desperately wanting to listen to his euphonious voice singing new songs! Asking him to sing new songs seems unrealistic, for he passed away a long time ago, but that doesn’t mean that we can’t write some codes about that!

3. Task Dependencies

I could ask for a new song if he were alive, though how do we know he has lyrics for a new song? I don’t think it was so easy for him to find his source of inspiration so quickly that he wrote lyrics for the songs that he hasn’t sung.

He would need to write the lyrics first. This is where we need dependencies! If we include the task dependencies in our code, he can sing new songs for us, in another world.

To create dependencies, we need “=>” sign, which will let our task(:sing_a_song) depend on the execution of another one (: write_the_lyrics) in this case.

Finishing my blog post here, I hope I could give you some insight into the basics of Rake. You can check this link to learn more about Rake:

https://web.archive.org/web/20071024171055/http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial#how

I will be glad if you leave comments/ask questions. I will try my best to reply to them.

Thank you for reading! 🐥 💻

--

--