在我的响应参数中,我尝试获取@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
答案 0 :(得分:0)
字符串#包含?看起来就是您想要的东西,如果您在调用方法调用之前将日期转换为字符串并在字符串上下文中传递“ 11”,则可以访问它:
when (@start_date.to_s.include?('11'))
答案 1 :(得分:0)
将@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"
end