Sha256: a73fd6a994363333a8ceed09b0bf84ebc3058fd2c0b5f3debeed4a478c7b57c4

Contents?: true

Size: 1.15 KB

Versions: 93

Compression:

Stored size: 1.15 KB

Contents

require 'date'
class Meetup
  def self.days_of_week
    [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]
  end

  def self.weekday_number(weekday)
    days_of_week.index(weekday)
  end

  attr_reader :year, :number
  def initialize(number, year)
    @year = year
    @number = number
    @first = Date.new(year, number, 1)
    @eighth = Date.new(year, number, 8)
    @thirteenth = Date.new(year, number, 13)
    @fifteenth = Date.new(year, number, 15)
    @twenty_second = Date.new(year, number, 22)
    @last = Date.new(year, number, -1)
  end

  def day(weekday, schedule)
    case schedule
    when :teenth then
      @thirteenth + days_til(weekday, @thirteenth)
    when :first then
      @first + days_til(weekday, @first)
    when :second then
      @eighth + days_til(weekday, @eighth)
    when :third then
      @fifteenth + days_til(weekday, @fifteenth)
    when :fourth then
      @twenty_second + days_til(weekday, @twenty_second)
    when :last then
      @last - (7 - (self.class.weekday_number(weekday) - @last.wday)) % 7
    end
  end

  private

  def days_til(weekday, day)
    (self.class.weekday_number(weekday) - day.wday) % 7
  end
end

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
trackler-2.2.1.36 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.35 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.34 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.33 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.32 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.31 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.30 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.29 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.28 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.27 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.26 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.25 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.24 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.23 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.22 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.21 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.20 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.19 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.18 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.17 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb