From: Jamis Buck Date: 2004-02-05T06:25:25+09:00 Subject: Re: flip flop operator proposal (from condition to condition do ) nobu.nokada@softhome.net wrote: >Hi, > >At Wed, 4 Feb 2004 15:23:10 +0900, >Jamis Buck wrote in [ruby-talk:91521]: > > >>In general, you want a parser that extracts everything between BEGIN and >>END, but there are a few cases where you want to treat the word >>'special' as the end instead, and then ignore everything until the next >>BEGIN. >> >>while line = file.gets >> if in line=~/BEGIN/ .. line=~/END/ => state >> state.set_state( line =~ /special/ ) >> if state.get_state >> # process line >> end >> end >>end >> >> > >Will set_state turn the state true, or false? Seeing the next >line, set_state(true) makes get_state to return false... > > > Well, what I had envisioned was the following: FlipFlip#get_state: return the current truth value of the flip flop. FlipFlop#set_state( state ): set the truth value of the flip flop And you're right -- there's a typo in my example. :( The set_state call should be: state.set_state( line !~ /special/ ) >>Like I said, it's pretty contrived, and would be better solved by >>choosing smarter guard conditions. However, it demonstrates a situation >>where exposing an interface for manipulating a flip-flop's state may be >>useful. >> >> > >Rather, what about binding a flip-flop operator to a local >variable? > > Fascinating concept--that would certainly be simpler. The only benefit I can see to using an object as opposed to a local variable binding is that you could pass the flipflop object into a method and have any manipulations made on it in the method be effective outside the method. -- Jamis Buck jgb3@email.byu.edu ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'