3

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?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354

2 Answers2

4

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.

Community
  • 1
  • 1
Kzu
  • 783
  • 4
  • 10
  • 1
    @Leem.fin This means that the task depends on your rails environment, which is therefore loaded first – Stobbej Nov 01 '11 at 13:51
1

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.

Holger Just
  • 52,918
  • 14
  • 115
  • 123