Class: Lafcadio::Month
- Inherits:
-
Object
- Object
- Lafcadio::Month
- Includes:
- Comparable
- Defined in:
- lib/lafcadio/dateTime/Month.rb
Overview
Represents a specific month in time. With the exception of Month.monthNames (which returns a zero-based array), every usage of the month value assumes that 1 equals January and 12 equals December.
Instance Attribute Summary collapse
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
-
.monthNames ⇒ Object
Returns an array of the full names of months (in English).
Instance Method Summary collapse
-
#+(amountToAdd) ⇒ Object
Returns a new Month that is
amountToAdd
months later. -
#-(amountToSubtract) ⇒ Object
Returns a new Month that is
amountToSubtract
months earlier. -
#<=>(anOther) ⇒ Object
Compare this Month to another Month.
-
#endDate ⇒ Object
Returns the last Date of the month.
-
#eql?(anOther) ⇒ Boolean
Is this Month equal to
anOther
?anOther
must be another Month of the same value. -
#hash ⇒ Object
Calculate a hash value for this Month.
-
#initialize(month = nil, year = nil) ⇒ Month
constructor
A new month can be set to a specific
month
andyear
, or you can call Month.new with no arguments to receive the current month. -
#next ⇒ Object
Returns the next Month.
-
#prev ⇒ Object
Returns the previous Month.
-
#startDate ⇒ Object
Returns the first Date of the month.
-
#to_s ⇒ Object
Returns a string of the format “January 2001”.
Constructor Details
#initialize(month = nil, year = nil) ⇒ Month
A new month can be set to a specific month
and year
, or you can call Month.new with no arguments to receive the current month.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lafcadio/dateTime/Month.rb', line 19 def initialize( month = nil, year = nil ) require 'date' if month.nil? || year.nil? date = Date.today month = date.mon unless month year = date.year unless year end fail "invalid month" if month < 1 || month > 12 @month = month @year = year end |
Instance Attribute Details
#month ⇒ Object (readonly)
Returns the value of attribute month.
15 16 17 |
# File 'lib/lafcadio/dateTime/Month.rb', line 15 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
15 16 17 |
# File 'lib/lafcadio/dateTime/Month.rb', line 15 def year @year end |
Class Method Details
.monthNames ⇒ Object
Returns an array of the full names of months (in English). Note that “January” is the 0th element, and “December” is the 11th element.
8 9 10 11 |
# File 'lib/lafcadio/dateTime/Month.rb', line 8 def Month.monthNames [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] end |
Instance Method Details
#+(amountToAdd) ⇒ Object
Returns a new Month that is amountToAdd
months later.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lafcadio/dateTime/Month.rb', line 57 def +( amountToAdd ) ( fullYears, remainingMonths ) = amountToAdd.divmod( 12 ) resultYear = @year + fullYears resultMonth = @month + remainingMonths if resultMonth > 12 resultMonth -= 12 resultYear += 1 end Month.new( resultMonth, resultYear ) end |
#-(amountToSubtract) ⇒ Object
Returns a new Month that is amountToSubtract
months earlier.
69 70 71 |
# File 'lib/lafcadio/dateTime/Month.rb', line 69 def -(amountToSubtract) self + (-amountToSubtract) end |
#<=>(anOther) ⇒ Object
Compare this Month to another Month.
32 33 34 35 36 37 38 |
# File 'lib/lafcadio/dateTime/Month.rb', line 32 def <=>(anOther) if @year == anOther.year @month <=> anOther.month else @year <=> anOther.year end end |
#endDate ⇒ Object
Returns the last Date of the month.
89 90 91 |
# File 'lib/lafcadio/dateTime/Month.rb', line 89 def endDate self.next.startDate - 1 end |
#eql?(anOther) ⇒ Boolean
Is this Month equal to anOther
? anOther
must be another Month of the same value.
52 53 54 |
# File 'lib/lafcadio/dateTime/Month.rb', line 52 def eql?(anOther) self == anOther end |
#hash ⇒ Object
Calculate a hash value for this Month.
46 47 48 |
# File 'lib/lafcadio/dateTime/Month.rb', line 46 def hash "#{@year}#{@month}".to_i end |
#next ⇒ Object
Returns the next Month.
79 80 81 |
# File 'lib/lafcadio/dateTime/Month.rb', line 79 def next self + 1 end |
#prev ⇒ Object
Returns the previous Month.
74 75 76 |
# File 'lib/lafcadio/dateTime/Month.rb', line 74 def prev self - 1 end |
#startDate ⇒ Object
Returns the first Date of the month.
84 85 86 |
# File 'lib/lafcadio/dateTime/Month.rb', line 84 def startDate Date.new( @year, @month, 1 ) end |
#to_s ⇒ Object
Returns a string of the format “January 2001”.
41 42 43 |
# File 'lib/lafcadio/dateTime/Month.rb', line 41 def to_s Month.monthNames[@month-1][0..2] + " " + @year.to_s end |