Stussa
Stussa

Reputation: 3415

Namespaces in Ruby Rake Tasks

Are the following equivalent?

namespace :resque do
  task setup: :environment do
  end
end

task "resque:setup" => :environment do
end

Upvotes: 4

Views: 1467

Answers (1)

Lee Jarvis
Lee Jarvis

Reputation: 16241

In short: yes. When running rake resque:setup both of these tasks will be invoked.

Rake will merge these tasks. You can test this by doing the following:

p Rake.application.tasks

Which in this case would return something like

[<Rake::Task resque:setup => [environment]>]

Which is simply an Array holding a single Rake::Task object. You can also check the scope or list of namespaces for a task by doing:

p Rake.application.tasks.first.scope
  #=> ["resque"]

If you want to learn a little more on how the internals of Rake work, check out Rake::Task and Rake::TaskManager

Upvotes: 3

Related Questions