I have a Rakefile
with a Rake task that I would normally call from the command line:
rake blog:post Title
I'd like to write a Ruby script that calls that Rake task multiple times, but the only solution I see is shelling out using `` (backticks) or system
.
What's the right way to do this?
This works with Rake version 10.0.3:
As knut said, use
reenable
if you want to invoke multiple times.You can use
invoke
andreenable
to execute the task a second time.Your example call
rake blog:post Title
seems to have a parameter. This parameter can be used as a parameter ininvoke
:Example:
Please replace
mytask
withblog:post
and instead the task definition you canrequire
your rakefile.This solution will write the result to stdout - but you did not mention, that you want to suppress output.
Interesting experiment:
You can call the
reenable
also inside the task definition. This allows a task to reenable himself.Example:
The result (tested with rake 10.4.2):
In a script with Rails loaded (e.g.
rails runner script.rb
)from timocracy.com: