1

我正在写一个 rake 任务。问题是我想在if keeper.has_trevance_info? && candidate.has_trevance_info?为真时停止执行任务。我只想让它打印Another record already has that info!在日志中并停止任务。我该怎么做?会是raise还是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
4

1 回答 1

2

您不想使用异常处理来退出任务。您可以使用“中止”或“退出”。

所以你的代码会做这样的事情:

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

或者:

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
于 2013-07-23T20:35:37.707 回答