Class: Month
Defined Under Namespace
Modules: Methods
Constant Summary collapse
- NAMES =
{ 1 => :January, 2 => :February, 3 => :March, 4 => :April, 5 => :May, 6 => :June, 7 => :July, 8 => :August, 9 => :September, 10 => :October, 11 => :November, 12 => :December }
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #+(number) ⇒ Object
- #-(object) ⇒ Object
- #<=>(month) ⇒ Object
- #dates ⇒ Object
- #downto(min, &block) ⇒ Object
- #end_date ⇒ Object
- #eql?(object) ⇒ Boolean
- #hash ⇒ Object
- #include?(date) ⇒ Boolean (also: #===)
-
#initialize(year, number) ⇒ Month
constructor
A new instance of Month.
- #length ⇒ Object
- #name ⇒ Object
- #next ⇒ Object (also: #succ)
- #start_date ⇒ Object
- #step(limit, step = 1) ⇒ Object
- #to_s ⇒ Object
- #upto(max, &block) ⇒ Object
Constructor Details
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
29 30 31 |
# File 'lib/month.rb', line 29 def number @number end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
29 30 31 |
# File 'lib/month.rb', line 29 def year @year end |
Class Method Details
Instance Method Details
#+(number) ⇒ Object
105 106 107 108 109 |
# File 'lib/month.rb', line 105 def +(number) a, b = (@number - 1 + number).divmod(12) self.class.new(@year + a, b + 1) end |
#-(object) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/month.rb', line 111 def -(object) if object.is_a?(Integer) self + (-object) else (year * 12 + @number) - (object.year * 12 + object.number) end end |
#<=>(month) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/month.rb', line 53 def <=>(month) if @year == month.year @number <=> month.number else @year <=> month.year end end |
#dates ⇒ Object
133 134 135 |
# File 'lib/month.rb', line 133 def dates start_date .. end_date end |
#downto(min, &block) ⇒ Object
101 102 103 |
# File 'lib/month.rb', line 101 def downto(min, &block) step(min, -1, &block) end |
#end_date ⇒ Object
129 130 131 |
# File 'lib/month.rb', line 129 def end_date Date.new(@year, @number, -1) end |
#eql?(object) ⇒ Boolean
49 50 51 |
# File 'lib/month.rb', line 49 def eql?(object) object.class == self.class && object.hash == self.hash end |
#hash ⇒ Object
45 46 47 |
# File 'lib/month.rb', line 45 def hash [@year, @number].hash end |
#include?(date) ⇒ Boolean Also known as: ===
119 120 121 |
# File 'lib/month.rb', line 119 def include?(date) @year == date.year && @number == date.month end |
#length ⇒ Object
137 138 139 |
# File 'lib/month.rb', line 137 def length end_date.mday end |
#name ⇒ Object
35 36 37 |
# File 'lib/month.rb', line 35 def name NAMES.fetch(@number) end |
#next ⇒ Object Also known as: succ
63 64 65 66 67 68 69 |
# File 'lib/month.rb', line 63 def next if @number == 12 self.class.new(@year + 1, 1) else self.class.new(@year, @number + 1) end end |
#start_date ⇒ Object
125 126 127 |
# File 'lib/month.rb', line 125 def start_date Date.new(@year, @number, 1) end |
#step(limit, step = 1) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/month.rb', line 73 def step(limit, step = 1) raise ArgumentError if step.zero? unless block_given? return enum_for(:step, limit, step) end month = self if step > 0 until month > limit yield month month += step end else until month < limit yield month month += step end end end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/month.rb', line 31 def to_s "#@year-#{@number.to_s.rjust(2, '0')}" end |
#upto(max, &block) ⇒ Object
97 98 99 |
# File 'lib/month.rb', line 97 def upto(max, &block) step(max, 1, &block) end |