1

I am writing a rake task. The thing is that I want to stop execution of the task when if keeper.has_trevance_info? && candidate.has_trevance_info? is true. I just want it to print Another record already has that info! in the log and stop the task. How would I do that? Would it be raise or throw?

  billing_infos.each do |candidate|
    unless keeper.nil?
      raise "Another record already has that info! Manually compare BillingInfo ##{keeper.id}
             and ##{candidate.id}" if keeper.has_trevance_info? && candidate.has_trevance_info?

    else
      keeper = candidate
    end

  end
lurker
  • 56,987
  • 9
  • 69
  • 103
bigpotato
  • 26,262
  • 56
  • 178
  • 334

1 Answers1

2

You don't want to use exception handling to exit the task. You can use 'abort' or 'exit'.

So your code would do something like this:

billing_infos.each do |candidate|
  unless keeper.nil?
    if keeper.has_trevance_info? && candidate.has_trevance_info?
      puts "Another record already has that info! Manually compare BillingInfo ##{keeper.id}"
      exit
    end
  else
    keeper = candidate
  end
end

Or:

billing_infos.each do |candidate|
  unless keeper.nil?
    abort("Another record already has that info! Manually compare BillingInfo ##{keeper.id}") if keeper.has_trevance_info? && candidate.has_trevance_info?
  else
    keeper = candidate
  end
end
lurker
  • 56,987
  • 9
  • 69
  • 103
  • thanks! exit/abort was the method I was looking for! Is there a difference between the two? – bigpotato Jul 23 '13 at 20:36
  • @Edmund the `abort` lets you specify the message. Outside of that I think they have the same affect. There's a good discussion here: http://stackoverflow.com/questions/2316475/how-do-i-return-early-from-a-rake-task – lurker Jul 23 '13 at 20:38