【发布时间】:2012-09-11 22:52:59
【问题描述】:
不可能从循环more than once 中invoke 相同的 rake 任务。但是,我希望能够调用rake first 并循环遍历一个数组,并在每次迭代中使用不同的参数调用second。由于invoke 只在第一次执行,我尝试使用execute,但Rake::Task#execute 不使用splat (*) 运算符,只接受一个参数。
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。还有其他(更好的)方法来完成我想做的事情吗?
【问题讨论】: