From: Tadayoshi Funaba Date: 1999-07-09T01:58:22+09:00 Subject: [ruby-list:15323] Re: 年月を範囲でうまく扱うには? ふなばです。 僕もためしに書いてみました。date2 の焼き直しです。 ごとけんさんのと大して変らないし :-) class Mon include Comparable def initialize(mn=0) @mn = mn end class << self def mon_to_mn(y, m) (y + 4712) * 12 + m - 1 end def mn_to_mon(mn) [mn / 12 - 4712, mn % 12 + 1] end def exist2? (y, m) mn = mon_to_mn(y, m) if [y, m] == mn_to_mon(mn) mn end end alias_method :exist?, :exist2? def new2(y=-4712, m=1) unless mn = exist2?(y, m) fail ArgumentError, 'invalid mon' end new(mn) end def this() new(mon_to_mn(*(Time.now.to_a[4..5].reverse))) end end def mn() @mn end def year() Mon.mn_to_mon(@mn)[0] end def mon() Mon.mn_to_mon(@mn)[1] end def + (other) case other when Numeric; return Mon.new(@mn + other) end fail TypeError, 'expected numeric' end def - (other) case other when Numeric; return Mon.new(@mn - other) when Mon; return @mn - other.mn end fail TypeError, 'expected numeric or mon' end def <=> (other) case other when Numeric; return @mn <=> other when Mon; return @mn <=> other.mn end fail TypeError, 'expected numeric or mon' end def downto(min) @mn.downto(min.mn) do |mn| yield Mon.new(mn) end end def upto(max) @mn.downto(max.mn) do |mn| yield Mon.new(mn) end end def step(max, step) @mn.step(max.mn, step) do |mn| yield Mon.new(mn) end end def succ() self + 1 end def eql? (other) self == other end def hash() @mn end def to_s() format('%04d-%02d', year, mon) end end if __FILE__ == $0 Mon.new2(2000,1).downto(Mon.new2(1999,7)) do |mn| puts mn end end で、これ書いて気がついたんですが、いつのまにか、Range って upto でなく、 succ を呼ぶようになってたんですねえ。コードを修正しなくては。 ('b'*0+'J'+'u'+'y'*0+'s'+'t'+' '+'v'*0+ 'a'+'n'+'o'+'p'*0+'t'+'h'+'e'+'r'+' '+ 'c'*0+'R'+'i'*0+'u'+'j'*0+'b'+'y'+' '+ 'h'+'a'*1+'c'+'k'+'e'+'r'+',').display ふなば ただよし