Rubyでインクリメント演算子
モドキ。ちょっと%が多め。
n = Counter.new puts n #=> 0 n%%%++% # インクリメント! puts n #=> 1 n%%%--% # デクリメント! puts n #=> 0
↓ネタのタネ
class Counter def initialize count = 0 @count = count end def % op if op == '++' @count += 1 self elsif op == '--' @count -= 1 self else Count.new @count % op end end def + n Count.new @count + n end def - n Count.new @count - n end def to_s @count.to_s end end