我有一个继承的 Ruby 脚本,它读取一个 csv 文件,其中包含具有这种格式的开始时间和结束时间的“电视节目”:
start_time = 20:00:00
end_time = 20:45:00
目标是根据开始和结束时间为每个电视节目分配一个“时隙”(以下值之一):
23:00:00 - 05:00:00 = Late Night = l
05:00:00 - 09:00:00 = Morning = m
09:00:00 - 17:00:00 = Day Time = d
17:00:00 - 20:00:00 = Evening = e
20:00:00 - 23:00:00 = Prime = p
现在我有一个巨大的 if/else 语句,大约 100 行 Ruby 代码:
if(start_time >= 50000 && start_time < 90000) #start time is between 5 and 9 am
if(end_time <= 90000)
@timeSlot = ["Morning"]
puts "timeSlot = [Morning]"
elsif(end_time <= 170000 && end_time > 90000)
@timeSlot = ["Morning", "Daytime"]
puts "timeSlot = [Morning, Daytime]"
elsif(end_time <= 200000 && end_time > 90000 && end_time > 170000)
@timeSlot =["Morning", "Daytime", "Evening"]
puts "timeSlot =[Morning, Daytime, Evening]"
elsif(end_time <= 230000 && end_time > 90000 && end_time > 170000 && end_time > 200000)
@timeSlot =["Morning", "Daytime", "Evening", "Prime"]
puts "timeSlot =[Morning, Daytime, Evening, Prime]"
else
@timeSlot =["Morning", "Daytime", "Evening", "Prime", "LateNight"]
puts "timeSlot =[Morning, Daytime, Evening, Prime, LateNight]"
end
elsif (start_time >= 90000 && start_time < 170000)
.........
........
end
我试图更改实现,以便代码易于维护、扩展和阅读。我对这个问题的第一次尝试是使用 excel 中的矩阵直观地解决它,如图所示。
这是直观显示的问题。现在的问题是如何以有效的方式在代码中做到这一点?
欢迎任何建议