Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. class Meetup
  2. require 'pry'
  3. WEEKDAY = [:sunday,:monday,:tuesday,:wednesday,:thursday, :friday, :saturday]
  4. WEEKS = [:first,:second,:third,:fourth,:last]
  5. LENGTH_OF_WEEK = 7
  6. def initialize(month, year)
  7. @month = month
  8. @year = year
  9. @input_date = Date.new(year,month)
  10. end
  11.  
  12. def day(weekday,week)
  13. if week == :teenth
  14. (@input_date + 12).step(@input_date + 18) do |date|
  15. if WEEKDAY[date.wday] == weekday
  16. return date
  17. end
  18. end
  19. else
  20. first_day_of_month = @input_date.wday
  21. shifted_week = WEEKDAY.rotate(first_day_of_month)
  22. days_shifted = shifted_week.index(weekday)
  23. nth_week = (Date.new(year, month + 1).prev_day.cweek - @input_date.cweek if week == :last
  24. return @input_date + (LENGTH_OF_WEEK * WEEKS.index(week)) + days_shifted
  25. end
  26. end
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement