# Ruby Rake 'advanced' rule arguments

**URL:** <https://www.ruby-forum.com/t/ruby-rake-advanced-rule-arguments/157367>\
**Category:** Ruby\
**Created:** [January 10, 2009, 12:52am UTC](https://www.ruby-forum.com/t/ruby-rake-advanced-rule-arguments/157367 "2009-01-10T00:52:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [January 10, 2009, 12:52am UTC](https://www.ruby-forum.com/t/ruby-rake-advanced-rule-arguments/157367/1 "2009-01-10T00:52:56Z")

</div>

Just starting out with Rake and Ruby, and made a little rakefile  
(below).

It does a simple dependency based file copy of source files to a “dist”  
directory. I works fine, but I was wondering if there is a way to  
retrieve the “regex” argument to Rake::rule in the proc that generates  
or modifies the “task\_name”. If so I wouldn’t have to add a variable  
(testregex in rakefile) in the scope that calls “rule”. Or maybe a way  
to access $’ $` and $& from the regex test that matches the rule’s  
target (task\_name)

If this is better posted to another list let me know ;^\>

PK

# \*\*\*\* rakefile \*\*\*\*

task :default =\> [:test]

LIBSRCDIR = ‘…/…/common’

# I didn’t see this in the “FileList”

def addprefix(prefix,files)  
a = FileList.new() # can one allocate to files.size?  
files.each\_index { |i|  
a[i] = prefix + files[i];  
}  
return(a)  
end

# gotta do what ya gotta do

def regexifyPath(str)  
return(str.gsub(///,’/’).  
gsub(/./,’.’))  
end

# copy in source for AiroNetSampleLib.dll

NETLIBSRC = [  
‘HTTPClient.cpp’,  
‘HTTPInputStream.cpp’,  
‘HTTPChunkedOutputStream.cpp’,  
‘HTTPClientImpl.h’,  
]

NETLIBDIR = ‘./NetSampleLib’  
directory NETLIBDIR

NETLIBSRC\_FULL = addprefix(NETLIBDIR + ‘/’, NETLIBSRC)

testregex = Regexp.new(’^’ + regexifyPath(NETLIBDIR + ‘/’))

rule( testregex =\> [  
proc { |task\_name| task\_name.sub(testregex, LIBSRCDIR + ‘/aironet/’)  
}  
]) do |t|  
puts “cp #{t.source} #{t.name}”  
cp t.source, t.name  
end

task :test =\> NETLIBSRC\_FULL do |t|  
puts “building #{t.name}”  
puts “from #{t.prerequisites}”  
end

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [January 10, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/ruby-rake-advanced-rule-arguments/157367/2 "2009-01-10T22:08:41Z")

</div>

Anyway - yes I’m learning … answered my own question  
The miracle of subroutines and scope solves the problem and factors out  
nicely.

The below make a rule to copy tarket "[destdir]/file"s from srcdir to  
destdir

* * *

def copyRule(destdir, srcdir)

if(srcdir =~ //\z/)  
srcdir = $`  
end

if(destdir =~ //\z/)  
# rake doesn’t check both, string based  
directory(destdir)  
srcdir = $`  
end

directory(destdir)

regex =  
Regexp.new(’^’ + regexifyPath(destdir) + ‘/[^/]+\z’)

Rake::Task::create\_rule( regex =\> [  
proc { |task\_name|  
task\_name =~ //[^/]+\z/  
task\_name = srcdir + $&  
}  
]) do |t|  
cp t.source, t.name  
end  
end

Peter I myself wrote:

> Just starting out with Rake and Ruby, and made a little rakefile  
> (below).
