Class: Year

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year) ⇒ Year

Returns a new instance of Year.



6
7
8
# File 'lib/year.rb', line 6

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

Instance Attribute Details

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Instance Method Details

#+(x) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/year.rb', line 24

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



14
15
16
17
18
19
20
21
22
# File 'lib/year.rb', line 14

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

#<=>(other) ⇒ Object



10
11
12
# File 'lib/year.rb', line 10

def <=>(other)
  self.year <=> other.year
end

#datesObject



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

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

#first_dateObject



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

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

#first_monthObject



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

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

#first_weekdayObject



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

def first_weekday; first_month.first_weekday; end

#last_dateObject



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

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

#last_monthObject



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

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

#last_weekdayObject



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

def last_weekday; last_month.last_weekday; end

#monthsObject



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

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

#weekdaysObject



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

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