Rake执行多个参数

我在一个任务中调用一个rake任务,当涉及到调用execute时,我遇到了障碍

response = Rake::Task["stuff:sample"].execute[:match => "HELLO"] 

要么

 response = Rake::Task["stuff:sample"].execute[:match => "HELLO",:freq=>'100'] 

调用任务

 task :sample, [:match,:freq] => :environment do |t, args| 

我得到的错误是“无法将Hash转换为整数”

有任何想法吗?

我认为问题在于您没有发布的代码。 适合我的工作:

 james@James-Moores-iMac:/tmp$ head r.rb call.rb ==> r.rb <== task :sample, [:match,:freq] do |t, args| puts "hello world" puts args[:match] end ==> call.rb <== require 'rubygems' require 'rake' load 'r.rb' Rake::Task["sample"].execute :match => "HELLO" james@James-Moores-iMac:/tmp$ ruby call.rb hello world HELLO james@James-Moores-iMac:/tmp$ 

执行语法中的方括号让我感到困惑。 这是一种特殊的rake语法(你可能使用不正确)或者你的意思是发送一个带有一个元素的数组(一个哈希)? 这不是一样的吗?

 response = Rake::Task["sample"].execute([:match => "HELLO",:freq=>'100']) 

除此之外, Task#execute需要Rake:TaskArguments。

 class TaskArguments ... # Create a TaskArgument object with a list of named arguments # (given by :names) and a set of associated values (given by # :values). :parent is the parent argument object. def initialize(names, values, parent=nil) 

你可以使用:

 stuff_args = {:match => "HELLO", :freq => '100' } Rake::Task["stuff:sample"].execute(Rake::TaskArguments.new(stuff_args.keys, stuff_args.values)) 

您还可以使用Task#invoke,它将接收基本的args。 如果多次调用,请确保您Task#reenable