# Custom rake task

**URL:** <https://www.ruby-forum.com/t/custom-rake-task/192291>\
**Category:** IronRuby\
**Created:** [July 27, 2010, 8:59pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291 "2010-07-27T20:59:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 27, 2010, 8:59pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/1 "2010-07-27T20:59:15Z")

</div>

I was following Derek Bailey’s article from yesterday entitled “How to  
Build Custom Rake Tasks; The Right Way”  
\<[http://www.lostechies.com/blogs/derickbailey/archive/2010/07/26/how-to-](http://www.lostechies.com/blogs/derickbailey/archive/2010/07/26/how-to-)  
build-custom-rake-tasks-the-right-way.aspx\> and tried to replicate some  
of his code in IronRuby. Now I’ve run into a snag that I’m hoping  
somebody can help with.

My .rb file looks like so:

require ‘rake’

def foo(\*args)

body = proc {

```
puts 'hello world'

```

}

Rake::Task.define\_task(\*args, body)

end

foo :bar

task :default =\> [:bar]

Pretty simple, right? Well, when I run it using rake, I get the  
following error:

C:\Users\txg0\Desktop\>rake -f test1.rb --trace

(in C:/Users/txg0/Desktop)

rake aborted!

syntax error, unexpected IDENTIFIER, expecting AMPERSAND

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `load’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in  
`raw\_load\_rakefile’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2017:in  
`load\_rakefile’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in  
`standard\_exception\_handling’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2016:in  
`load\_rakefile’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2000:in `run’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in  
`standard\_exception\_handling’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/bin/rake:31

C:/Program Files (x86)/IronRuby 1.0v4/bin/rake:19:in `load’

C:/Program Files (x86)/IronRuby 1.0v4/bin/rake:19

Anyone have any clues?

Confidentiality Notice: This e-mail message, including any attachments,  
is for the sole use of the intended recipient(s) and may contain  
confidential and privileged information. Any unauthorized review, copy,  
use, disclosure, or distribution is prohibited. If you are not the  
intended recipient, please contact the sender by reply e-mail and  
destroy all copies of the original message.

---

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 27, 2010, 9:43pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/2 "2010-07-27T21:43:10Z")

</div>

The sample doesn’t run in MRI, either 🙂

You need an ampersand in front of “body” when passing to define\_task:

Rake::Task.define\_task(\*args, &body)

Sean

---

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 27, 2010, 9:44pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/3 "2010-07-27T21:44:55Z")

</div>

You’re missing an ampersand (&) on the define\_task call. It should be:  
Rake::Task.define\_task(\*args, \*&\*body)

## Shay.

Shay F. | Microsoft Visual C#/IronRuby MVP | Author of IronRuby  
Unleashed  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)

---

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 27, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/4 "2010-07-27T22:00:44Z")

</div>

To pass the body proc into define\_task, you need to turn it into a block  
by calling &body. I would also recommend using a lambda instead of a  
proc (due to return semantics). I’ll also be turning my WIX rake tasks  
into a gem, so there will be more examples there.

* * *

From: Goode, Troy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, July 27, 2010 11:59 AM  
To: removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [Ironruby-core] custom rake task

I was following Derek Baileyâ€™s article from yesterday entitled â€œHow to  
Build Custom Rake Tasks; The Right  
Wayâ€[http://www.lostechies.com/blogs/derickbailey/archive/2010/07/26/how-to-build-custom-rake-tasks-the-right-way.aspx](http://www.lostechies.com/blogs/derickbailey/archive/2010/07/26/how-to-build-custom-rake-tasks-the-right-way.aspx)  
and tried to replicate some of his code in IronRuby. Now Iâ€™ve run into a  
snag that Iâ€™m hoping somebody can help with.

My .rb file looks like so:

require ‘rake’

def foo(\*args)  
body = proc {  
puts ‘hello world’  
}  
Rake::Task.define\_task(\*args, body)  
end

foo :bar

task :default =\> [:bar]

Pretty simple, right? Well, when I run it using rake, I get the  
following error:

C:\Users\txg0\Desktop\>rake -f test1.rb --trace  
(in C:/Users/txg0/Desktop)  
rake aborted!  
syntax error, unexpected IDENTIFIER, expecting AMPERSAND  
C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `load' C:/Program Files (x86)/IronRuby 1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `raw\_load\_rakefile’  
C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2017:in  
`load_rakefile' C:/Program Files (x86)/IronRuby 1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard\_exception\_handling’  
C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2016:in  
`load_rakefile' C:/Program Files (x86)/IronRuby 1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2000:in `run’  
C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in  
`standard_exception_handling' C:/Program Files (x86)/IronRuby 1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run’  
C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/bin/rake:31  
C:/Program Files (x86)/IronRuby 1.0v4/bin/rake:19:in `load’  
C:/Program Files (x86)/IronRuby 1.0v4/bin/rake:19

Anyone have any clues?

Confidentiality Notice: This e-mail message, including any attachments,  
is for the sole use of the intended recipient(s) and may contain  
confidential and privileged information. Any unauthorized review, copy,  
use, disclosure, or distribution is prohibited. If you are not the  
intended recipient, please contact the sender by reply e-mail and  
destroy all copies of the original message.

---

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 27, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/5 "2010-07-27T22:00:44Z")

</div>

When you give a method a block in Ruby (which is really what you’re  
doing  
here), and you’ve defined it as a Proc, you need to pass the variable  
holding the Proc with an ampersand in front of it. This is because while  
code blocks and Procs conceptually do the same thing, they are not. The  
ampersand tells Ruby “hey, convert this to a code block for me”.

–  
Will G.  
[http://hotgazpacho.org/](http://hotgazpacho.org/)

---

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 27, 2010, 10:16pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/6 "2010-07-27T22:16:33Z")

</div>

Specifically, if you do an explicit return (use the return keyword) from  
a  
Proc, that exits the scope in which the Proc is executed. lambdas that  
return do not exit the scope in which it was executed.

## See [http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby](http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby) [http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby](http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby)

Will G.

> **[hotgazpacho](http://hotgazpacho.org/)**

---

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 28, 2010, 12:47am UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/7 "2010-07-28T00:47:35Z")

</div>

“Splat! is the star (\*) operator, typically used in Ruby for defining  
methods that take an unlimited number of arguments”

[http://pivotallabs.com/users/nick/blog/articles/438-ruby-pearls-vol-1-the-splat](http://pivotallabs.com/users/nick/blog/articles/438-ruby-pearls-vol-1-the-splat)

## [http://pivotallabs.com/users/nick/blog/articles/438-ruby-pearls-vol-1-the-splat](http://pivotallabs.com/users/nick/blog/articles/438-ruby-pearls-vol-1-the-splat)It must appear as the last argument in a method signature, as it collects all values from its position on into an array. [http://www.manning.com/black2/](http://www.manning.com/black2/)

Will G.

> **[hotgazpacho](http://hotgazpacho.org/)**

---

<div class="post-metadata">

**Author:** ![GoodeS\_Troy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@GoodeS\_Troy](https://www.ruby-forum.com/u/GoodeS_Troy)\
**Post date:** [July 27, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/custom-rake-task/192291/8 "2010-07-27T23:20:57Z")

</div>

Thanks for the help everyone! I had been trying multiple variations of  
the code I sent, removing/adding asterisks & ampersands and just never  
stumbled upon the correct combo. Thanks again for the help!

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Will G.  
Sent: Tuesday, July 27, 2010 4:12 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] custom rake task

Specifically, if you do an explicit return (use the return keyword) from  
a Proc, that exits the scope in which the Proc is executed. lambdas that  
return do not exit the scope in which it was executed.

See [http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby](http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby)

–  
Will G.  
[http://hotgazpacho.org/](http://hotgazpacho.org/)

On Tue, Jul 27, 2010 at 3:35 PM, Jim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

To pass the body proc into define\_task, you need to turn it into a block  
by calling &body. I would also recommend using a lambda instead of a  
proc (due to return semantics). I’ll also be turning my WIX rake tasks  
into a gem, so there will be more examples there.

* * *

From: Goode, Troy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, July 27, 2010 11:59 AM  
To: removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [Ironruby-core] custom rake task

I was following Derek Baileyâ€™s article from yesterday entitled â€œHow to  
Build Custom Rake Tasks; The Right Wayâ€  
[http://www.lostechies.com/blogs/derickbailey/archive/2010/07/26/how-to-build-custom-rake-tasks-the-right-way.aspx](http://www.lostechies.com/blogs/derickbailey/archive/2010/07/26/how-to-build-custom-rake-tasks-the-right-way.aspx)  
and tried to replicate some of his code in IronRuby. Now Iâ€™ve run into a  
snag that Iâ€™m hoping somebody can help with.

My .rb file looks like so:

require ‘rake’

def foo(\*args)

body = proc {

```
puts 'hello world'

```

}

Rake::Task.define\_task(\*args, body)

end

foo :bar

task :default =\> [:bar]

Pretty simple, right? Well, when I run it using rake, I get the  
following error:

C:\Users\txg0\Desktop\>rake -f test1.rb --trace

(in C:/Users/txg0/Desktop)

rake aborted!

syntax error, unexpected IDENTIFIER, expecting AMPERSAND

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `load’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in  
`raw\_load\_rakefile’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2017:in  
`load\_rakefile’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in  
`standard\_exception\_handling’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2016:in  
`load\_rakefile’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2000:in `run’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in  
`standard\_exception\_handling’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run’

C:/Program Files (x86)/IronRuby  
1.0v4/lib/ironruby/gems/1.8/gems/rake-0.8.7/bin/rake:31

C:/Program Files (x86)/IronRuby 1.0v4/bin/rake:19:in `load’

C:/Program Files (x86)/IronRuby 1.0v4/bin/rake:19

Anyone have any clues?

Confidentiality Notice: This e-mail message, including any attachments,  
is for the sole use of the intended recipient(s) and may contain  
confidential and privileged information. Any unauthorized review, copy,  
use, disclosure, or distribution is prohibited. If you are not the  
intended recipient, please contact the sender by reply e-mail and  
destroy all copies of the original message.

* * *

Ironruby-core mailing list  
removed\_email\_address@domain.invalid  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

Confidentiality Notice: This e-mail message, including any attachments,  
is for the sole use of the intended recipient(s) and may contain  
confidential and privileged information. Any unauthorized review, copy,  
use, disclosure, or distribution is prohibited. If you are not the  
intended recipient, please contact the sender by reply e-mail and  
destroy all copies of the original message.
