Leem.fin
Leem.fin

Reputation: 42582

Newbie, about Rake task syntax

In Rake task definition, like following:

desc 'SOME description'
  task :some_task => :environment do
    # DO SOMETHING
  end

What does the :some_task in task :some_task => :environment means?

Is it the method name which will be invoked in the DO SOMETHING part?

Can :some_task be any arbitrary string which describe the task?

Upvotes: 3

Views: 1864

Answers (2)

Kzu
Kzu

Reputation: 783

In fact, when you're creating a rake task, :some_task is the name of the task you are calling.

For instance, in this case, you will call rake some_task

You also could define namespaces for your tasks :

namespace :my_tasks do
  desc "My first task"
  task :first_task => :environment do
    # DO SOMETHING
  end
end

And then you will call rake my_tasks:first_task in your console.

Hope it will help you,

Edit:

As explained by Holger Just, the :environment executes the "environment" task and if you are on rails, loads the environment. This could take a long time but il also helps you if your tasks works with the database.

Upvotes: 4

Holger Just
Holger Just

Reputation: 55718

With your example, you define a task called some_task which can be invoked by calling rake some_task on the command line.

It will depend on the environment task which will be rune before your new some_task. In rails, the environment task sets up the rails environment (loading libraries, preparing database connection, ...) which is quite expensive and thus optional.

Upvotes: 1

Related Questions