【问题标题】:Passing arguments to a method in Rake Task [duplicate]将参数传递给 Rake 任务中的方法 [重复]
【发布时间】:2015-08-17 23:49:46
【问题描述】:

我有一个 Rake 任务,其中我从命令行获取两个参数,我需要将这些参数传递给任务内部的方法。我该怎么做?

namespace :auto_sales_commission do
   desc "Process for Daily Summary Commission"
   task :daily_sales_commission,[:arg1, :arg2] => :environment do |t,args|
        #some code
   end

   def get_date(arg1,arg2)
        #some code
   end
end

【问题讨论】:

  • 我不确定为什么这是重复的,因为我不确定这个问题如何解决我的问题。如果可以,请详细说明。
  • 在接受答案的第一个示例中,您可以看到 args 可以作为散列访问,因此 args[:arg1] 将产生第一个参数,依此类推。

标签: ruby-on-rails ruby command-line-arguments rake-task


【解决方案1】:

您可以像哈希参数 (1) 一样访问参数:

task :daily_sales_commission, [:arg1, :arg2] do |t,args|
  #args can be treated like a hash.
  get_date(args[:arg1], args[:arg2]) #<- call your method
end

def get_date(arg1,arg2)
    #some code
end

这些参数不是常用的命令行参数,必须直接作为rake参数的参数使用。该示例将 1 和 2 作为参数值:

rake daily_sales_commission[1,2]

(1) 它不是一个 Hash,它是一个 Rake::TaskArguments-object。但您也可以使用[] 访问参数。

【讨论】:

    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2011-07-02
    • 2012-05-18
    • 1970-01-01
    相关资源
    最近更新 更多