Skip to content

Instantly share code, notes, and snippets.

@gouf
Created August 16, 2019 01:12
Show Gist options
  • Save gouf/cb640f86afb8d89d0d3a5601c99d211d to your computer and use it in GitHub Desktop.
Save gouf/cb640f86afb8d89d0d3a5601c99d211d to your computer and use it in GitHub Desktop.
Paiza レベルアップ問題集: 「日付のフォーマット Ruby編」 - https://paiza.jp/works/mondai/dateset/ruby/date_format
class DateFormat
DATE_ORDER = %r{[0-9]{4}/[0-9]{2}/[0-9]{2}}.freeze
MONTH = [*(1..12).map { |n| n.to_s.rjust(2, '0') }].freeze
DAY = [*(1..31).map { |n| n.to_s.rjust(2, '0') }].freeze
class << self
def validate(date_string)
return 'No' unless date_string.match?(DATE_ORDER)
_year, month, day = date_string.split('/')
return 'No' unless MONTH.include?(month)
return 'No' unless DAY.include?(day)
'Yes'
end
end
end
puts DateFormat.validate(gets.chomp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment