Class: Year

Inherits:
Object
  • Object
show all
Defined in:
lib/year.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year) ⇒ Year

Returns a new instance of Year.



4
5
6
# File 'lib/year.rb', line 4

def initialize(year)
  @year = year.to_i
end

Instance Attribute Details

#yearObject

Returns the value of attribute year.



2
3
4
# File 'lib/year.rb', line 2

def year
  @year
end

Instance Method Details

#+(x) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/year.rb', line 18

def +(x)
  if x.is_a?(Numeric)
    return self.class.new(@year + x)
  elsif x.is_a?(Year) 
    return @year + x.year
  else
    raise TypeError, 'expected numeric or year'
  end
end

#-(x) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/year.rb', line 8

def -(x)
  if x.is_a?(Numeric)
    return self.class.new(@year - x)
  elsif x.is_a?(Year) 
    return @year - x.year
  else
    raise TypeError, 'expected numeric or year'
  end
end

#datesObject



33
# File 'lib/year.rb', line 33

def dates; (first_date..last_date).to_a; end

#first_dateObject



35
# File 'lib/year.rb', line 35

def first_date; Date.new(year,1,1); end

#first_monthObject



30
# File 'lib/year.rb', line 30

def first_month; Month.new(year,1); end

#first_weekdayObject



40
# File 'lib/year.rb', line 40

def first_weekday; first_month.first_weekday; end

#last_dateObject



36
# File 'lib/year.rb', line 36

def last_date; Date.new(year,12,31); end

#last_monthObject



31
# File 'lib/year.rb', line 31

def last_month; Month.new(year,12); end

#last_weekdayObject



41
# File 'lib/year.rb', line 41

def last_weekday; last_month.last_weekday; end

#monthsObject



28
# File 'lib/year.rb', line 28

def months; (first_month..last_month).to_a; end

#weekdaysObject



38
# File 'lib/year.rb', line 38

def weekdays; (first_weekday..last_weekday).to_a; end