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.
27 28 29 |
# File 'lib/month.rb', line 27 def number @number end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
27 28 29 |
# File 'lib/month.rb', line 27 def year @year end |
Class Method Details
Instance Method Details
#+(number) ⇒ Object
103 104 105 106 107 |
# File 'lib/month.rb', line 103 def +(number) a, b = (@number - 1 + number).divmod(12) self.class.new(@year + a, b + 1) end |
#-(object) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/month.rb', line 109 def -(object) if object.is_a?(Integer) self + (-object) else (year * 12 + @number) - (object.year * 12 + object.number) end end |
#<=>(month) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/month.rb', line 51 def <=>(month) if @year == month.year @number <=> month.number else @year <=> month.year end end |
#dates ⇒ Object
131 132 133 |
# File 'lib/month.rb', line 131 def dates start_date .. end_date end |
#downto(min, &block) ⇒ Object
99 100 101 |
# File 'lib/month.rb', line 99 def downto(min, &block) step(min, -1, &block) end |
#end_date ⇒ Object
127 128 129 |
# File 'lib/month.rb', line 127 def end_date Date.new(@year, @number, -1) end |
#eql?(object) ⇒ Boolean
47 48 49 |
# File 'lib/month.rb', line 47 def eql?(object) object.class == self.class && object.hash == self.hash end |
#hash ⇒ Object
43 44 45 |
# File 'lib/month.rb', line 43 def hash [@year, @number].hash end |
#include?(date) ⇒ Boolean Also known as: ===
117 118 119 |
# File 'lib/month.rb', line 117 def include?(date) @year == date.year && @number == date.month end |
#length ⇒ Object
135 136 137 |
# File 'lib/month.rb', line 135 def length end_date.mday end |
#name ⇒ Object
33 34 35 |
# File 'lib/month.rb', line 33 def name NAMES.fetch(@number) end |
#next ⇒ Object Also known as: succ
61 62 63 64 65 66 67 |
# File 'lib/month.rb', line 61 def next if @number == 12 self.class.new(@year + 1, 1) else self.class.new(@year, @number + 1) end end |
#start_date ⇒ Object
123 124 125 |
# File 'lib/month.rb', line 123 def start_date Date.new(@year, @number, 1) end |
#step(limit, step = 1) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/month.rb', line 71 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
29 30 31 |
# File 'lib/month.rb', line 29 def to_s "#@year-#{@number.to_s.rjust(2, '0')}" end |
#upto(max, &block) ⇒ Object
95 96 97 |
# File 'lib/month.rb', line 95 def upto(max, &block) step(max, 1, &block) end |