# Needing help using Rake from within a Ruby program

**URL:** <https://www.ruby-forum.com/t/needing-help-using-rake-from-within-a-ruby-program/126425>\
**Category:** Ruby\
**Created:** [January 18, 2008, 5:17am UTC](https://www.ruby-forum.com/t/needing-help-using-rake-from-within-a-ruby-program/126425 "2008-01-18T05:17:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 18, 2008, 5:17am UTC](https://www.ruby-forum.com/t/needing-help-using-rake-from-within-a-ruby-program/126425/1 "2008-01-18T05:17:14Z")

</div>

Hello all,

I’m trying to invoke rake from within one of my Unit Test files to setup  
a  
test database before running tests and I’m having some trouble doing so.  
I  
try the following code in a file that is located in the same directory  
as my  
Rakefile:

rake = Rake::Application.new  
rake[‘foo:bar:load’].invoke

and I get the following:

RuntimeError: Don’t know how to build task ‘foo:bar:load’

However, if I run rake from the command line in this same directory  
passing  
it the foo:bar:load task it works fine. Any suggestions?

Thanks! – BTR

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 18, 2008, 5:32am UTC](https://www.ruby-forum.com/t/needing-help-using-rake-from-within-a-ruby-program/126425/2 "2008-01-18T05:32:26Z")

</div>

On Jan 17, 2008 10:14 PM, Bryan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and I get the following:
> 
> RuntimeError: Don’t know how to build task ‘foo:bar:load’
> 
> However, if I run rake from the command line in this same directory passing  
> it the foo:bar:load task it works fine. Any suggestions?
> 
> Thanks! – BTR

Did you require the rakefile in you code? rake does so automatically.

Thanks,

Gordon

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 18, 2008, 5:37am UTC](https://www.ruby-forum.com/t/needing-help-using-rake-from-within-a-ruby-program/126425/3 "2008-01-18T05:37:41Z")

</div>

> Did you require the rakefile in you code? rake does so automatically.
> 
> Thanks,
> 
> Gordon

Wow, that was very terse. Even for me. What I mean is that the rake  
command will require your rakefile automatically. But if you want to  
use a rake task from ruby, you will have to explicitly require the  
rakefile.

Thanks,

Gordon

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 18, 2008, 5:54am UTC](https://www.ruby-forum.com/t/needing-help-using-rake-from-within-a-ruby-program/126425/4 "2008-01-18T05:54:39Z")

</div>

Gordon,

How do I specify the Rakefile to use?

Thanks! – BTR

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [January 18, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/needing-help-using-rake-from-within-a-ruby-program/126425/5 "2008-01-18T13:40:30Z")

</div>

On Jan 17, 2008 10:53 PM, Bryan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Gordon,
> 
> How do I specify the Rakefile to use?
> 
> Thanks! – BTR

I was a little off. I don’t think you can use require, but you can use  
load.

require ‘rubygems’  
require ‘rake’  
load ‘path/to/Rakefile’

rake = Rake::Application.new  
rake[‘spec’].invoke
