From: Jamis Buck Date: 2004-02-04T09:21:49+09:00 Subject: Re: flip flop operator proposal (from condition to condition do ) Phil Tomson wrote: > >Another idea would be to have a 'from (condition) to (condition)' >statement that looks looks like: > from l=~/BEGIN/ to l=~/END/ do > #do something in this range > end > > What if we just use the keyword 'in' to introduce a flipflop, and retain the range-ish syntax to separate the conditions? Something like this: if in l=~/BEGIN/ .. l=~/END/ ... end The advantage of using a boolean phrase is that you can use it in conditions, where it is most common. That also lets you do something when the flipflop is false, which the 'from..do' construct you suggested doesn't (easily) allow. Since getting at the object that encapsulates the state of the flipflop will be less common (one might even say "rare", although its hard to say at this point since neither Perl nor Ruby allow it), the common case (just getting the boolean result) should be simple and straightforward. Getting at the object might be done via something like this: in l=~/BEGIN/ .. l=~/END/ => state The expression itself still returns boolean, but the object encapsulating the flipflop is placed in 'state'. I took the 1.8.1 source and tried hacking at it to introduce this syntax, and managed to get the 'in' keyword working as above. Problem is, I'm not a bison expert, and I'm certainly not very familiar with the inner workings of Ruby's parser, so I keep getting 2 reduce/reduce conflicts caused by the 'in' keyword... not sure how to fix that. And when I try to use '=>' to assign the object, I get 27 shift/reduce conflicts (which I think I understand, but I'm not sure how to fix). I'd be happy to send a patch to anyone interested, but it's really pretty trivial, and (as I said) imperfect. I tried introducing new keywords to fill these roles, but the means by which Ruby does its keyword lookup is pretty arcane, and it is not immediately obvious how to add a new keyword. Any gurus of Ruby's inner workings that could clarify how to do this? I'd be especially interested in hearing from the proponents of the flip-flop operator in Ruby as to whether this suggestion would "fit the bill." If it isn't something they'd be comfortable using, then it is probably not the most appropriate solution to the problem. However, I think it gets the best of both worlds -- it is clearer that you're dealing with more than just a range, and you can use it in more places than simply in conditions. >I'm just sort of brainstorming here, but I think the 'from .. to .. >{}' statement is very readable and the intent is quite clear and it >just might be able to replace the current flipflop operators. > > I agree, 'from .. to ...' is very readable, but perhaps not as convenient given the regular usage of the flip-flop operator. Then again, I'd like to hear from someone who uses the flip-flop a lot as to whether they feel it would be cumbersome. Readability is good, but if you take it too far you get VB and Pascal, which are easy to learn, but clunky (IMO) to actually write software in. -- 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"'