简体   繁体   English

在Ruby on Rails中,rake任务是从另一个rake文件中调用一个函数

[英]In Ruby on Rails rake task is calling a function from another rake file

As you can see, I've defined a function inside a rake file. 如您所见,我已经在rake文件中定义了一个函数。 No problem, that works fine. 没问题,那很好。 Problem is, when I declare def get_user_input in another rake file . 问题是,当我在另一个rake文件中声明def get_user_input时。 In that case the function gets called from another .rake file Can you suggest anything? 在这种情况下, 将从另一个.rake文件调用函数。您能建议什么吗? Thanks. 谢谢。

namespace :backtest do

  def get_user_input
    if ENV['date_from'].present? && ENV['date_until'].present? 
      # get input...
    else
      abort 'Sample usage: blah blah...'
    end
  end

  desc "Start backtest"
  task :start => :environment do
    get_user_input
    # rest of the code...
  end
end

you can do this: 你可以这样做:

require 'rake'
load 'your_rake_file_name_here.rake'
get_user_input

Moved the functions into a module and the problem gone. 将功能移至模块中,问题消失了。

namespace :backtest do

 Module MY
  def self.get_user_input
    if ENV['date_from'].present? && ENV['date_until'].present? 
      # get input...
    else
      abort 'Sample usage: blah blah...'
    end
  end
 end

 desc "Start backtest"
 task :start => :environment do
   My.get_user_input
   # rest of the code...
 end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM