Search code examples
ruby-on-railsfunctionrakedrycode-duplication

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. No problem, that works fine. Problem is, when I declare def get_user_input in another rake file. In that case the function gets called from another .rake file Can you suggest anything? 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

Solution

  • 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