Blog
This is my blog, I write about technology and other things, read it!

Why you will fall in love with Rake

Introduction

I love Rake

Rake is an utility to build and execute tasks.
I knew Rake when I began to develop with Ruby on Rails, but quite immediately I fell in love with it because it can be very useful every day, the only limit you have is your imagination.
When you write a task you are writing Ruby code and you can include everything you need (gems and so on) so your task can really do everything you want.
Another killer feature of Rake is that the way to write tasks are very easy and it allows you to write many tasks in small amount of time. You can write tasks that executes other tasks too, so you can project your tasks in an efficient way!
At the moment, in my opinion, Rake is the most confortable way to write small utility in Ruby and during the last years I used it so many times! You will fall in love too, I’m sure!

How to get it

Install Rake is very simple, I imagine you have Ruby and Rubygems installed on your system (if you have not them you can google for it, there are so many guides for every OS and many OSes has installed them by default). The installation process requires just a simple command in your console:

$ sudo gem install rake

(in some cases you won’t need sudo, but more often you will)

Is also possible to install it from the source but I think it’s better to use Rubygems because in this way you will be able to use others gems in your tasks later.

How it works

Rake is a command used to execute some tasks you define. We will see how to write tasks in a minute, for the moment is enought to say that you write a plain text file in the directory with your tasks and you can decide which one execute.
The only requisite is that the file is named Rakefile, in this way Rake command will get it automatically and you will have just to choose which task execute.

How to write tasks

I told you that you have just to write a text file, how to write it? Another time the answer is “it’s easy!”.

Open your favourite text editor and paste this text:

task :hello do
  puts "Hello world!"
end

And save the file as “Rakefile”, now if you execute

$ rake hello

in console (in the same directory where you placed your Rakefile) you will see our “Hello world!” message. You have wrote your first Rake task!

Another useful feature is that you can give to every task a description, in order to remember what every task does without having to open Rakefile.
To do that in our :hello task you will add

desc "This task doesn't do anything, it's just an example!"

Now if you execute

$ rake -T

you will see all defined tasks (for the moment just one) with its name and its description! It will be very useful when tasks will increase in number.

After in this article I told you that you can use every gem you want in your tasks, let me show you an example using faker gem (it’s a gem to generate fake data)

require 'rubygems'
require 'faker'

desc "This task outputs a fake name"
task :random_name do
  puts Faker::Name.name
end

Now if you execute in your console

$ rake -T

you will see

rake random_name # This task outputs a fake name

And if you execute

$ rake random_name

You will see a name generated by the faker gem. Simple!

Now we can look at an example in which a task executes others tasks

require 'rubygems'
require 'faker'

desc "This task outputs a fake person"
task :random_person => [:random_name, :random_address, :random_phone]

desc "This task outputs a fake name"
task :random_name do
  puts Faker::Name.name
end

desc "This task outputs a fake address"
task :random_address do
  puts Faker::Address.street_name + " - " + Faker::Address.uk_postcode + " " + Faker::Address.city + " " + Faker::Address.uk_country
end

desc "This task outputs a fake phone"
task :random_phone do
  puts Faker::PhoneNumber.phone_number
end

We defined :random_name, :random_address and :random_phone tasks and :random_person that executes others three ones. Now if we execute

$ rake -T

we can see we have four tasks

rake random_address # This task outputs a fake address
rake random_name # This task outputs a fake name
rake random_person # This task outputs a fake person
rake random_phone # This task outputs a fake phone

every tasks does a specific thing, but if we execute :random_person we get three tasks executed with just a command.
Rake allows you to do many things and it’s impossible to cover everything, but if you look at Rake’s documentation you will find many others interesting way to use it.

What can I do with it?

As I told you before, the only limit is your imagination, I often use it for commands I don’t remember, so for example one of my tasks is

desc 'Clears DNS cache of OSX'
task :dns_flush do
  out = system 'dscacheutil -flushcache'
  puts "Flush: #{out}"
end

it executes an OSX (I’m on a Mac) command to clear DNS cache, I never remember the command so now I have just to do

$ rake -T

in my home and I get a list of what I’m looking for :)
Another useful task I created is the one that interacts with rsync to do backup of my documents, it doesn’t just execute rsync command but also creates the list of file to ignore.

Conclusion

I’m sure you will discover some great ways to use rake in your daily workflow and if you want to say me what you use rake for I’ll be happy to hear it from you!

Have a nice day!

6 Comments


  1. Janis
    Dec 25, 2010

    Like your writing! Still you can do some things to improve it.


  2. nurse practitioner
    Dec 31, 2010

    Wow this is a great resource.. I’m enjoying it.. good article


  3. free government grants
    Jan 11, 2011

    This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!


  4. educational grants
    Jan 24, 2011

    Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.


  5. mike
    Apr 15, 2011

    thanksthanksthanks


  6. Alonso Hoosock
    Oct 12, 2011

    hello there and thank you for your info – I’ve definitely picked up something new from right here. I did however expertise some technical points using this site, as I experienced to reload the web site a lot of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and could look out for a lot more of your respective intriguing content. Ensure that you update this again soon..

Leave a Reply