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

Ruby - Check to see if specific int exists in an API response parameter

在我的响应参数中,我试图获取 @start_date(这是一个 DateTime 变量,所以它是一个 int)并尝试查看它是否包含 11日期。 @start_date输出如下:

2018-11-04 02:00:00 -0600

这是我试图将其包装在其中的 case 语句

  #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

字符串#include?看起来就是您想要的,如果您在调用方法调用之前将日期转换为字符串并在字符串上下文中传入“11”,则可以访问它:

    when (@start_date.to_s.include?('11'))

@start_dateDateTime 您可以使用 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