Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Meetup
- require 'pry'
- WEEKDAY = [:sunday,:monday,:tuesday,:wednesday,:thursday, :friday, :saturday]
- WEEKS = [:first,:second,:third,:fourth,:last]
- LENGTH_OF_WEEK = 7
- def initialize(month, year)
- @month = month
- @year = year
- @input_date = Date.new(year,month)
- end
- def day(weekday,week)
- if week == :teenth
- (@input_date + 12).step(@input_date + 18) do |date|
- if WEEKDAY[date.wday] == weekday
- return date
- end
- end
- else
- first_day_of_month = @input_date.wday
- shifted_week = WEEKDAY.rotate(first_day_of_month)
- days_shifted = shifted_week.index(weekday)
- nth_week = (Date.new(year, month + 1).prev_day.cweek - @input_date.cweek if week == :last
- return @input_date + (LENGTH_OF_WEEK * WEEKS.index(week)) + days_shifted
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement