けいじゅ@日本ラショナルソフトウェアです. 最近メイルが溜っていてなかなかついていけていません(;_; In [ruby-list :06289 ] the message: "[ruby-list:6289] Re: i++ ", on Feb/05 17:20(JST) aito / ei5sun.yz.yamagata-u.ac.jp writes: >あ伊藤です.またまた本質に関係ないチャチャですが > >> >など書かなければいけない局面はかなり多いです。というか ++ が欲しい >> >と思うのはこの時しかないかも。 >> そうそう。私もほとんどがこういうケースです。 > >ということは,こんなのがあればいいんだったりして? > >class Counter > def initialize(i = 0) > @i = i > end > def incr; @i += 1 end > def decr; @i -= 1 end > def to_s; @i.to_s end > def to_i; @i end >end こんなの作ってみました. Counter#inc Counter#dec 以外に, 一応四則演算などができるようになっています. rbc0> c = Counter() #<Counter: 0> rbc0> c.inc #<Counter: 1> rbc0> c + 2 #<Counter: 3> rbc0> 2 + c 3 rbc0> c = c * c #<Counter: 1> rbc0> c #<Counter: 1> rbc0> c += 2 #<Counter: 3> -- ここから -- # # counter.rb - # $Release Version: 0.5$ # $Revision: 1.1 $ # $Date: 1998/02/17 08:39:02 $ # by Keiju ISHITSUKA(Nippon Rational Inc.) # # -- # # # def Counter(start = 0) Counter.new(start) end class Counter < Numeric def initialize(start = 0) @count = start end def inc(skip = 1) @count += skip end def dec(skip = 1) @count -= skip end def + (a) Counter(@count + a) end def - (a) Counter(@count - a) end def * (a) Counter(@count * a) end def / (a) Counter(@count / a) end def ** (other) Counter(@count ** other) end def % (other) Counter(@count % other) end def divmod(other) return Counter(@count / other), Counter(@count % other) end def abs Counter(@count.abs) end def <=> (other) @count <=> other end def coerce(other) case other when Counter return other.to_i, @count when Numeric return other, @count else super end end def to_i Integer(@count) end def to_f @count.to_f end def to_s "#" + @count.to_s end def inspect sprintf("#<Counter: %d>", @count) end def hash @count end end __ ................................石塚 圭樹@日本ラショナルソフトェア... ----------------------------------->> e-mail: keiju / rational.com <<---