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.56 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.55 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.54 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.53 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.52 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.51 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.50 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.49 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.48 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.47 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.46 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.45 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.44 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.43 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.42 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.41 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.40 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.39 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.38 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb
trackler-2.2.1.37 tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb