Class: Month
Overview
Month represents a specific month in time. With the exception of Month.month_names (which returns a zero-based array), every usage of the month value assumes that 1 equals January and 12 equals December.
Constant Summary collapse
- Version =
'0.1.0'
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
-
.month_names ⇒ 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.
-
#end_date ⇒ 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(year = nil, month = 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.
-
#start_date ⇒ Object
Returns the first Date of the month.
-
#to_s ⇒ Object
Returns a string of the format “January 2001”.
Constructor Details
#initialize(year = nil, month = 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.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/month.rb', line 20 def initialize( year = nil, month = 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.
16 17 18 |
# File 'lib/month.rb', line 16 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
16 17 18 |
# File 'lib/month.rb', line 16 def year @year end |
Class Method Details
.month_names ⇒ 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.
9 10 11 12 |
# File 'lib/month.rb', line 9 def Month.month_names [ "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.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/month.rb', line 33 def +( amountToAdd ) ( fullYears, remainingMonths ) = amountToAdd.divmod( 12 ) resultYear = @year + fullYears resultMonth = @month + remainingMonths if resultMonth > 12 resultMonth -= 12 resultYear += 1 end Month.new( resultYear, resultMonth ) end |
#-(amountToSubtract) ⇒ Object
Returns a new Month that is amountToSubtract
months earlier.
45 46 47 |
# File 'lib/month.rb', line 45 def -(amountToSubtract) self + (-amountToSubtract) end |
#<=>(anOther) ⇒ Object
Compare this Month to another Month.
50 51 52 53 54 55 56 |
# File 'lib/month.rb', line 50 def <=>(anOther) if @year == anOther.year @month <=> anOther.month else @year <=> anOther.year end end |
#end_date ⇒ Object
Returns the last Date of the month.
59 60 61 |
# File 'lib/month.rb', line 59 def end_date self.next.start_date - 1 end |
#eql?(anOther) ⇒ Boolean
Is this Month equal to anOther
? anOther
must be another Month of the same value.
65 66 67 |
# File 'lib/month.rb', line 65 def eql?(anOther) self == anOther end |
#hash ⇒ Object
Calculate a hash value for this Month.
70 71 72 |
# File 'lib/month.rb', line 70 def hash "#{@year}#{@month}".to_i end |
#next ⇒ Object
Returns the next Month.
75 76 77 |
# File 'lib/month.rb', line 75 def next self + 1 end |
#prev ⇒ Object
Returns the previous Month.
80 81 82 |
# File 'lib/month.rb', line 80 def prev self - 1 end |
#start_date ⇒ Object
Returns the first Date of the month.
85 86 87 |
# File 'lib/month.rb', line 85 def start_date Date.new( @year, @month, 1 ) end |
#to_s ⇒ Object
Returns a string of the format “January 2001”.
90 91 92 |
# File 'lib/month.rb', line 90 def to_s Month.month_names[@month-1][0..2] + " " + @year.to_s end |