在响应参数中,我尝试接受@start_date (它是一个DateTime变量,所以它是一个int),并试图查看它是否在日期中包含了11。@start_date输出如下:
2018-11-04 02:00:00 -0600这就是我试图用的案例陈述。
#begin case statement to see whether the shift length will go
#back/forward
case
when (@start_date.include?(11))
season_logic = (60*60*9)
puts "The date is a Fall date - The shift will go back one hour"
when (@start_date.include?(3))
season_logic = (60*60*7)
puts "The date is a Spring date - The shift will go forward one hour "
else
raise "The season logic could not be calculated"
end
season_logic但是,我说这句话时出错了:
NoMethodError: undefined method 'include?' for # .
<DateTime:0x007fe5774957f0>
Did you mean? include发布于 2018-10-11 15:15:17
String#include?这看起来是您想要的,如果您在调用方法调用之前将日期转换为字符串并在字符串上下文中传递'11‘,则可以访问该日期:
when (@start_date.to_s.include?('11'))发布于 2018-10-11 18:14:19
如果@start_date是一个DateTime,您可以使用month方法进行比较:
case @start_date.month
when 3
season_logic = (60*60*7)
puts "The date is a Spring date - The shift will go forward one hour "
when 11
season_logic = (60*60*9)
puts "The date is a Fall date - The shift will go back one hour"
else
raise "The season logic could not be calculated"
endhttps://stackoverflow.com/questions/52763509
复制相似问题