Ruby-检查API响应参数中是否存在特定的int

时间:2018-10-11 15:12:03

标签: ruby api int case dst

在我的响应参数中,我尝试获取@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

2 个答案:

答案 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