From: Gavin Sinclair Date: 2004-02-01T21:57:27+09:00 Subject: Re: Save the flip/flop op! On Sunday, February 1, 2004, 11:29:50 PM, Robert wrote: >> Comments, Phil? Nathaniel? Others? > I wouldn't put it into File or IO since this is too specialized > functionality. It deserves a class of it's own: > class FlipFlop > def initialize(on, off) > @on, @off = on, off > @state = false > end > > def ===(o) > if @state > @state = false if @off === o > else > @state = true if @on === o > end > > @state > end > end Nice implementation. The usage is still more verbose than the FF operator, though. Perhaps some method to process an enumerable object? # Untested class FlipFlop def process(enum) enum.each do |o| yield(o) if self === o end end end File.open(path) do |io| FlipFlop.new(/BEGIN/, /END/).process(io) do |line| puts line end end Is that any good? Gavin