Class: Year
Instance Attribute Summary collapse
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
- #+(x) ⇒ Object
- #-(x) ⇒ Object
- #<=>(other) ⇒ 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.
6 7 8 |
# File 'lib/year.rb', line 6 def initialize(year) @year = year.to_i end |
Instance Attribute Details
#year ⇒ Object
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 |
#dates ⇒ Object
39 |
# File 'lib/year.rb', line 39 def dates; (first_date..last_date).to_a; end |
#first_date ⇒ Object
41 |
# File 'lib/year.rb', line 41 def first_date; Date.new(year,1,1); end |
#first_weekday ⇒ Object
46 |
# File 'lib/year.rb', line 46 def first_weekday; first_month.first_weekday; end |
#last_date ⇒ Object
42 |
# File 'lib/year.rb', line 42 def last_date; Date.new(year,12,31); end |
#last_weekday ⇒ Object
47 |
# File 'lib/year.rb', line 47 def last_weekday; last_month.last_weekday; end |
#months ⇒ Object
34 |
# File 'lib/year.rb', line 34 def months; (first_month..last_month).to_a; end |
#weekdays ⇒ Object
44 |
# File 'lib/year.rb', line 44 def weekdays; (first_weekday..last_weekday).to_a; end |