6 Rake Flashcards
What is Rake?
Rake is a task runner. Rake will execute different tasks (basically a set of Ruby code) specified in any
file with a .rake extension from the command line. To run a rake task, just call the Rake command
with the name of your task.
When do you use Rake?
Rake tasks are used when:
* Making a backup of your database
* Filling a database with data
* Running tests
* Gathering and reporting stats, etc.
How do you add a description for Rake tasks?
You can use the desc method to describe your tasks. This is done on the line right above the task
definition. It’s also what gives you that nice output when you run rake -T to get a list of tasks. Tasks
are displayed in alphabetical order.
Rakefile.rb
desc ‘Make coffee’
task :make_coffee do
puts ‘Made a cup of coffee. Shakes are gone.’
end
end
How do you execute a Rake task?
Rakefile.rb
namespace :morning do
task :turn_of_alarm
# …
end
$ rake morning:ready_for_the_day
…
Rakefile.rb
Rake::Task[‘morning:make_coffee’].execute
This always executes the task, but not its dependencies.
How do you pass arguments to a Rake task?
Rakefile.rb
namespace :morning do
desc ‘Make coffee’
task :make_coffee, :cups do |t, args|
cups = args.cups
puts “Made #{cups} cups of coffee. Shakes are gone.»
end
end
$ rake morning:make_coffee[2]
Made 2 cups of coffee. Shakes are gone.