ruby-on-railsrake-task

Using method within rake file tasks


A set of rake tasks of a .rake file are structured as follows

task :process_data  => :environment do
  CSV.foreach("scores.tsv", :col_sep => "\t", headers: true) do |row|
    begin
      [...]
       repeated_method_a
       ad-hoc_method
       repeated_method_b
    rescue StandardError => e
    end
  end
end

How should this rake file be structured to process sub-methods, such as:

def repeated_method_a
  do_its_thing
end

Solution

  • You can simply add it under your task in the same file, so you have this:

    task :process_data  => :environment do
      CSV.foreach("scores.tsv", :col_sep => "\t", headers: true) do |row|
        begin
          [...]
           repeated_method_a
           ad-hoc_method
           repeated_method_b
        rescue StandardError => e
        end
      end
    end
    
    def repeated_method_a
      do_its_thing
    end