在循环more than once内不可能对invoke
执行相同的rake任务。但是,我希望能够调用rake first
并循环访问一个数组,并在每次迭代时调用second
使用不同的参数。由于invoke
仅在第一次执行,因此我尝试使用execute
,但Rake::Task#execute不使用splat(*)运算符,只接受一个参数。周围如何多次使用参数执行Rake任务?
desc "first task"
task :first do
other_arg = "bar"
[1,2,3,4].each_with_index do |n,i|
if i == 0
Rake::Task["foo:second"].invoke(n,other_arg)
else
# this doesn't work
Rake::Task["foo:second"].execute(n,other_arg)
end
end
end
task :second, [:first_arg, :second_arg] => :prerequisite_task do |t,args|
puts args[:first_arg]
puts args[:second_arg]
# ...
end
一个黑客就是把参数execute
到一个数组和second
检查args来的结构,但似乎,那么,hackish的。是否有另一种(更好的)方法来完成我想要做的事情?
看起来很漂亮。谢谢! –