Ruby - проверьте, существует ли конкретный тип int в параметре ответа API - PullRequest
0 голосов
/ 11 октября 2018

В своем параметре ответа я пытаюсь взять @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

Ответы [ 2 ]

0 голосов
/ 11 октября 2018

Если @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
0 голосов
/ 11 октября 2018

Строка # включить?похоже, что вы идете, к которому вы можете получить доступ, если вы конвертируете дату в строку перед вызовом метода и передаете '11' в контексте строки:

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