Create  Edit  Diff  FrontPage  Index  Search  Changes  History  RSS  Source  Login

FrequentlyAskedQuestions - History


  • Added words are showed as here.
  • Removed words are showed as here.
//FrequentlyAskedQuestions
//4. Frequently Asked Questions
//4. よくある質問と回答
//!4.1 How can I call one Rake task from inside another task?
!4.1 あるタスクの中から別のタスクを呼び出すこには、どうすれば良いのでしょうか?

//Generally, if you want invoke one task from another task, the proper way to do that is to include the task to be invoked as a prerequisite of the task doing the invoking.
通常、あるタスク(:secondary と仮定)から、別のタスク(:primary と仮定)を実施するには、:secondary の前提条件として、:primary が実施済みであることを指定するのが、一般的です。

//For example:
*''例''

// task :primary => [:secondary]
//
// task :secondary do
//   puts "Doing Secondary Task"
// end
task :primary => [:secondary]

task :secondary do
   puts ":secondary の実施。"
end

//However, there are certain rare occasions where you want to invoke a task from within the body of a primary task. You could do it like this:
しかし、まれにではありますが、:primary タスクの中で、:secondary タスクを実行したいというケースも存在します。その場合は以下のようにして実現できます。

// task :primary do
//   Rake::Task[:secondary].invoke
// end
//
// task :secondary do
//   puts "Doing Secondary Task"
// end
task :primary do
   Rake::Task[:secondary].invoke
end

task :secondary do
   puts ":secondary の実施。"
end

//Keep the following in mind:
上記の方法を選んだ場合、以下を念頭に置いてください。

//* The secondary task will be invoked when the body of the primary task is executed.
* :primary タスクが実施される際に、その中で :secondary タスクが実施されます。
//* All prerequisites of the secondary task will be run before it body of the secondary task.
* :secondary タスクのすべての前提条件は、:primary の前ではなく、:secondary タスクの前に実施されます。
//* If the secondary task has already been run, it will not be run again, even though it has been explicitly invoked. Rake tasks can only be invoked once per run.
* もし、一度でも :secondary タスクが実施されれば、例え明示的に指定されても、再度実施されることはありません。Rake のタスクは、実行時に一度だけです

//If the second and third point above are not to your liking, then perhaps you should consider making the secondary task a regular Ruby method and just calling it directly. Something like this:
もし、二つめと三つ目の条件が気に入らない場合は、通常の Ruby のメソッドを使って :secondary タスクと :primary タスクで、そのメソッドを呼び出すことを考慮する必要があるでしょう。以下のようにです。

// task :primary do
//   secondary_task
// end
//
// task :secondary do
//   secondary_task
// end
//
// def secondary_task
//   puts "Doing Secondary Task"
// end
task :primary do
   secondary_task
end

task :secondary do
   secondary_task
end

def secondary_task
   puts ":secondary の実施。"
end

Last modified:2024/03/28 17:10:55
Keyword(s):
References: