Skip to content

Instantly share code, notes, and snippets.

@werdlerk
Last active February 11, 2016 21:35
Show Gist options
  • Save werdlerk/43af35fc5d595065df81 to your computer and use it in GitHub Desktop.
Save werdlerk/43af35fc5d595065df81 to your computer and use it in GitHub Desktop.
Launch School challenge: Meetup
class Meetup
def initialize(month, year)
@month, @year = month, year
end
def day(weekday, schedule)
@date = Date.new(@year, @month, 1)
wday = Date::DAYNAMES.index(weekday.to_s.capitalize)
case schedule
when :first
@date.next_weekday(wday)
when :second
@date.next_weekday(wday) + 7
when :third
@date.next_weekday(wday) + 14
when :fourth
@date.next_weekday(wday) + 21
when :last
@end_of_month = Date.new(@year, @month, 1).next_month - 1
@end_of_month.previous_weekday(wday)
when :teenth
@date = Date.new(@year, @month, 13)
@date.next_weekday(wday)
end
end
end
class Date
def next_weekday(wday)
self + ((wday - self.wday) % 7)
end
def previous_weekday(wday)
self - ((self.wday - wday) % 7)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment