[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Curious behavior of rake

Its Me

1/24/2005 10:20:00 PM

I came across a curious rake behavior, in which my rakefile required a file
that (accidentally) required the rakefile itself.

It makes rake execute tasks twice.

rake, version 0.4.15

--- rakefile---
require 'a'
task :one
puts :one
end
----a.rb---
require 'rakefile'
-----------

rake one
#=>
one
one



3 Answers

Jim Weirich

1/24/2005 11:43:00 PM

0

On Monday 24 January 2005 05:20 pm, itsme213 wrote:
> I came across a curious rake behavior, in which my rakefile required a file
> that (accidentally) required the rakefile itself.

Hmmm ... don't do that :)
> It makes rake execute tasks twice.

Yep. Rake loads the rakefile via the "load" command, require doesn't know
that the rake file is alread loaded. So loading it and requiring it cause
the file to be evaluated twice. Rake tasks are additive, e.g. you can say:

task :one do puts "ONCE" end
task :one do puts "TWICE" end

Since the file is evaluated twice, each task action is added to the task
twice. Therefore running any task executes the actions twice.

If there is stuff in the rakefile that the other file depends on, it is better
to extract it into a normal library file and let both the rakefile and the
other file require that third file.

Hope this helps.

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


Its Me

1/25/2005 1:54:00 AM

0


"Jim Weirich" <jim@weirichhouse.org> wrote
> Since the file is evaluated twice, each task action is added to the task
> twice. Therefore running any task executes the actions twice.
>
> Hope this helps.

Very clear now, thanks.

> If there is stuff in the rakefile that the other file depends on, it is
better
> to extract it into a normal library file and let both the rakefile and the
> other file require that third file.

I have had trouble previously before because of the number of Dir and/or
FileUtil methods 'included' by rake. Specifically name conflicts since
whatever rake includes goes into Object, and that conflicted with things I
do in my own code. I was wondering if there was a way around that global
'include', since uses of the Dir or FileUtil methods are (should be?)
limited to within tasks. For example, Tasks could catch missing methods that
are in the set of FileUtil.instance_methods, and handle them appropriately.
Feasible? Worth considering?




Jim Weirich

1/25/2005 2:48:00 AM

0

On Monday 24 January 2005 08:55 pm, itsme213 wrote:
> I have had trouble previously before because of the number of Dir and/or
> FileUtil methods 'included' by rake. Specifically name conflicts since
> whatever rake includes goes into Object, and that conflicted with things I
> do in my own code.

Generally rake assumes that it owns the "VM". It doesn't expect you to run
application code in the same ruby instance as rake is running. I know
Austin's test task does this, but goes against one of rake's basic
assumptions.

> I was wondering if there was a way around that global
> 'include', since uses of the Dir or FileUtil methods are (should be?)
> limited to within tasks. For example, Tasks could catch missing methods
> that are in the set of FileUtil.instance_methods, and handle them
> appropriately. Feasible? Worth considering?

An interesting idea. Its probably worth investigating. Thanks for the idea.

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)