Skip to content

Instantly share code, notes, and snippets.

@jorrizza
Last active December 20, 2015 16:09
Show Gist options
  • Save jorrizza/6159311 to your computer and use it in GitHub Desktop.
Save jorrizza/6159311 to your computer and use it in GitHub Desktop.
Pubquiz code
require 'date'
class PubQuizDate
class << self
def parse(str)
days = %w{zon maan dins woens donder vrij zater}
format, pattern = {
next_week: /\Avolgende week (\w+)dag\Z/,
after_that: /\Ade (\w+)dag daarna\Z/
}.find {|fmt, ptrn| str =~ ptrn}
wday = $~.to_a[1]
if !format || !days.include?(wday)
raise ArgumentError, "can't parse that"
end
case format
when :next_week
now = DateTime.now
@last_date = now + (7 - now.wday) + days.index(wday)
when :after_that
raise RuntimeError, "no previous date set" unless @last_date
delta = days.index(wday) - @last_date.wday
delta += 7 if delta < 1
@last_date += delta
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment