# Rake task dependencies question

**URL:** <https://www.ruby-forum.com/t/rake-task-dependencies-question/194785>\
**Category:** Ruby\
**Created:** [September 10, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/rake-task-dependencies-question/194785 "2010-09-10T16:28:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [September 10, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/rake-task-dependencies-question/194785/1 "2010-09-10T16:28:26Z")

</div>

Quick question about rake task dependencies. Suppose I have the  
following:

task :prepare\_to\_fight =\> :has\_weapons do  
…  
end

task :attack\_with\_gun =\> :prepare\_to\_fight do  
…  
end

task :attack\_with\_sword =\> :prepare\_to\_fight do  
…  
end

Now, by default, if I run “rake attack\_with\_gun attack\_with\_sword”, it  
recognizes that “prepare\_to\_fight” has already been called when it goes  
to  
run the second task. So it saves itself the trouble and skips it.

My question is, is there some way to override that? That is, I want to  
prepare\_to\_fight each time, but only define it in one place. Note also  
that  
:prepare\_to\_fight has a dependency, which I only want to mention in one  
place, so I can’t just put this in a method.

Nobody was injured in the making of this completely fictitious, and very  
violent-sounding example…

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [September 10, 2010, 6:20pm UTC](https://www.ruby-forum.com/t/rake-task-dependencies-question/194785/2 "2010-09-10T18:20:16Z")

</div>

On Fri, Sep 10, 2010 at 9:23 AM, Andrew W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> task :attack\_with\_sword =\> :prepare\_to\_fight do  
> place, so I can’t just put this in a method.
> 
> Nobody was injured in the making of this completely fictitious, and very  
> violent-sounding example…

I came up with two ways that seem to work. Not sure which is best

First way uses prepare\_to\_fight as a method, which invokes the task.  
Since  
it is a method, it will be invoked multiple times. Since it invokes the  
other task, it has that dependency, on :has\_weapons, and :has\_weapons  
behaves like a regular task. Downside is that it is clunky feeling,  
because  
you move prepare\_to\_fight from a task to a method executed inside the  
block.

task :has\_weapons do  
puts “has weapons”  
end

def prepare\_to\_fight  
Rake::Task[:has\_weapons].invoke  
puts “prepare to fight”  
end

task :attack\_with\_gun do  
prepare\_to\_fight  
puts “attack with gun”  
end

task :attack\_with\_sword do  
prepare\_to\_fight  
puts “attack with sword”  
end

* * *

Second way is mark the task as not having been invoked, each time it is  
invoked. Downside here is that it uses private API, so this is subject  
to  
change.

task :has\_weapons do  
puts “has weapons”  
end

task :prepare\_to\_fight =\> :has\_weapons do  
Rake::Task[:prepare\_to\_fight].instance\_eval { @already\_invoked = false  
}  
puts “prepare to fight”  
end

task :attack\_with\_gun =\> :prepare\_to\_fight do  
puts “attack with gun”  
end

task :attack\_with\_sword =\> :prepare\_to\_fight do  
puts “attack with sword”  
end

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [September 10, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/rake-task-dependencies-question/194785/3 "2010-09-10T19:29:56Z")

</div>

Hello,

> Now, by default, if I run “rake attack\_with\_gun attack\_with\_sword”, it  
> recognizes that “prepare\_to\_fight” has already been called when it goes to  
> run the second task. So it saves itself the trouble and skips it.
> 
> My question is, is there some way to override that?

I think you would like to look at the Rake::Task#reenable method:

~\> ri Rake::Task#reenable

## ---------------------------------------------------- Rake::Task#reenable reenable()

```
 Reenable the task, allowing its tasks to be executed if the task is
 invoked again.

```

Cheers,

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [September 10, 2010, 10:24pm UTC](https://www.ruby-forum.com/t/rake-task-dependencies-question/194785/4 "2010-09-10T22:24:59Z")

</div>

On Sep 10, 2010, at 07:23 , Andrew W. wrote:

> task :attack\_with\_sword =\> :prepare\_to\_fight do  
> place, so I can’t just put this in a method.  
> I think it is safe to say that what you’ve described is NOT a task  
> dependency anymore, and is just a reusable unit of code. In ruby, that’s  
> just a method:

def prepare\_to\_fight

# …

end

task :attack\_with\_gun do  
prepare\_to\_fight  
end

task :attack\_with\_sword do  
prepare\_to\_fight  
end
