Class: Year
- Inherits:
-
Object
- Object
- Year
- Defined in:
- lib/year.rb
Instance Attribute Summary collapse
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
- #+(x) ⇒ Object
- #-(x) ⇒ Object
- #dates ⇒ Object
- #first_date ⇒ Object
- #first_month ⇒ Object
- #first_weekday ⇒ Object
-
#initialize(year) ⇒ Year
constructor
A new instance of Year.
- #last_date ⇒ Object
- #last_month ⇒ Object
- #last_weekday ⇒ Object
- #months ⇒ Object
- #weekdays ⇒ Object
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
#year ⇒ Object
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 |
#dates ⇒ Object
33 |
# File 'lib/year.rb', line 33 def dates; (first_date..last_date).to_a; end |
#first_date ⇒ Object
35 |
# File 'lib/year.rb', line 35 def first_date; Date.new(year,1,1); end |
#first_month ⇒ Object
30 |
# File 'lib/year.rb', line 30 def first_month; Month.new(year,1); end |
#first_weekday ⇒ Object
40 |
# File 'lib/year.rb', line 40 def first_weekday; first_month.first_weekday; end |
#last_date ⇒ Object
36 |
# File 'lib/year.rb', line 36 def last_date; Date.new(year,12,31); end |
#last_month ⇒ Object
31 |
# File 'lib/year.rb', line 31 def last_month; Month.new(year,12); end |
#last_weekday ⇒ Object
41 |
# File 'lib/year.rb', line 41 def last_weekday; last_month.last_weekday; end |
#months ⇒ Object
28 |
# File 'lib/year.rb', line 28 def months; (first_month..last_month).to_a; end |
#weekdays ⇒ Object
38 |
# File 'lib/year.rb', line 38 def weekdays; (first_weekday..last_weekday).to_a; end |