我得到了下面的 Rake 任务,它每天运行一个来结算我的应用程序中的一些赌注。但我很确定它可以被简单化,所以 IF-THEN 语句可以替换为 s CASE 词干。
我假设多个 IF 语句的运行时间比使用 CASE 选项的相同进程要长。那是对的吗?
我只是不确定如何将它从 IF-THEN 转换为 CASE 选项..
task :settle => :environment do
@bets = Bet.where(:settle => false)
@bets.each do |bet|
if not bet.value.nil?
if bet.price.value > bet.value and bet.buy == true then
bet.profitloss = 10
bet.settle = true
bet.save
end
if bet.price.value < bet.value and bet.buy == false then
bet.profitloss = 10
bet.settle = true
bet.save
end
if bet.price.value > bet.value and bet.buy == false then
bet.profitloss = -10
bet.settle = true
bet.save
end
if bet.price.value < bet.value and bet.buy == true then
bet.profitloss = -10
bet.settle = true
bet.save
end
if bet.price.value = bet.value then
bet.profitloss = -10
bet.settled = true
bet.save
end
end
end
end