# Rake Rule name problem

**URL:** <https://www.ruby-forum.com/t/rake-rule-name-problem/151321>\
**Category:** Ruby\
**Created:** [October 20, 2008, 11:42am UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321 "2008-10-20T11:42:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 20, 2008, 11:42am UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/1 "2008-10-20T11:42:56Z")

</div>

I my rake file a rule does not seem to produce a correct name.

i.e.

rule ‘.c’ do |t|  
p t.name #prints out ‘.c’  
end

Should this not be the name of the file it is going to produce i.e.  
blah.c

Thanks,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 20, 2008, 11:45am UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/2 "2008-10-20T11:45:11Z")

</div>

That should read

> In my rake file a rule does not seem to produce a correct name.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 20, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/3 "2008-10-20T14:35:56Z")

</div>

Try:  
rule ‘.o’ =\> [’.c’] do |t|  
p t.source  
p t.name  
end

–wpd

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 21, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/4 "2008-10-21T15:57:15Z")

</div>

for the script:

* * *

task :default do  
@name = “do some stuff here to get path and name”  
Rake::Task[:go].invoke  
end

task :go =\>["#{@name}.c"]

rule ‘.c’ =\> [’.xml’] do |t|  
p t.name  
p t.source  
end

rule ‘.xml’ do  
#create the xml file in here  
end

* * *

I get the following results:

“.c”  
“.c.xml”

Any help as to why I don’t get the name of the file I’m creating or any  
other advice will be much appreciated.

Thanks,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 21, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/5 "2008-10-21T23:03:26Z")

</div>

> p t.source  
> end
> 
> rule ‘.xml’ do  
> #create the xml file in here  
> end
> 
> The misunderstanding is in how rakefiles get interpreted. A rakefile is  
> simply a bunch of Ruby code. When you invoke rake at the command line,  
> it  
> effectively loads a few libraries, which define some handy methods for  
> you,  
> and then hands your rakefile off to the Ruby interpreter.

One of those “handy methods” I mentioned was one called “task” which  
takes  
takes an argument (or two, or three) and a block. It records the first  
argument as the name of the task (and records things like the  
prerequisites  
and argument names if you pass more parameters) and it records the block  
to  
be executed if/when that named task gets invoked.

By default, after the Ruby interpreter interprets all of the Ruby code  
in  
the makefile, the rake utility invokes the task named :default.

So, in your case, when the interpreter hits the first “task” method  
call, it  
records the name, :default, and some code to be executed if/when the  
:default task is executed. Notice that the code inside the “do” block  
is  
not executed at this point.

When the interpreter hits the second “task” method call, it records the  
name, :go, and says that that task has a prerequisite of, hmmm… at  
that  
point, the interpreter doesn’t know anything about @name, so the :go  
task  
has a prerequisite of “.c”.

At least, that’s what I think is going on. Try replacing the line that  
reads:

task :go =\>["#{@name}.c"]

with a line that reads:

task :go =\>[“blah.c”]

and see if that gives you more insight into what’s going on.

I’m not sure I have any good suggestions for accomplishing what you want  
to  
do. One idea might be to recognize that a Rakefile is simply ruby code,  
and  
instead of defining a ‘.c’ =\> [’.xml’] rule, you could define a method:

def c\_to\_xml(source)  
sh blah blah blah  
end

and invoke #c\_to\_xml from your :default task.

hth

–wpd

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 23, 2008, 1:34pm UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/6 "2008-10-23T13:34:58Z")

</div>

ok I changed the previous script to this

* * *

task :default do  
@name = “do some stuff here to get path and name”

go\_task = task :go =\> ["{#name}.c"]  
go\_task.invoke  
end

rule ‘.c’ =\> [’.xml’] do |t|  
p t.name  
p t.source  
end

rule ‘.xml’ do  
#create the xml file in here  
end

* * *

This now works a treat, Patrick was largely right about the names of  
tasks getting assigned before the variable was getting a value.  
The solution was to programatticaly create the task inside my default  
task.

Many thanks for the help,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 22, 2008, 12:51pm UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/7 "2008-10-22T12:51:20Z")

</div>

> task :go =\>[“blah.c”]

Maybe it will work like that but it’s not useful. I don’t know until  
run time the name of the file I want to create.

> def c\_to\_xml(source)  
> sh blah blah blah  
> end

Would that not then defeat the purpose of rake, I could just use a  
standard ruby script and I would not get any of the features of rules  
and file tasks?

the :go task might well be the problem though, I might have to create  
that task programatticaly then invoke it.

Many thanks for helping,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 22, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/rake-rule-name-problem/151321/8 "2008-10-22T15:51:11Z")

</div>

On Wed, Oct 22, 2008 at 6:49 AM, Jim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > task :go =\>[“blah.c”]
> 
> Maybe it will work like that but it’s not useful. I don’t know until  
> run time the name of the file I want to create.

Sorry, I just meant that to be used as an example to show what was going  
on. From my understanding of your problem, I didn’t think it would be  
useful.

Would something like this solve your problem?

def figure\_out\_names

# This should return a list of names

end

# assuming that figure\_out\_names() returns the names w/o the “.c”

extension  
list\_of\_names = figure\_out\_names()  
list\_of\_names.each do |name|  
task name + “.c” =\> name + “.xml”  
end

task :default =\> list\_of\_names.map {|n| n + “.c”}

rule ‘.c’ =\> [‘.xml’] do |t|  
p t.name  
p t.source  
end

–wpd
