From: Dan Doel Date: 2004-02-01T06:51:54+09:00 Subject: Re: No difference between .. and ... flip/flop operators? Phil Tomson wrote: >identical as in there is no difference in behavior between them? > Yes, that's what I meant. As for all the stuff about what to replace it with and such (not just the parent post, but many others)... Some of the examples given can be done almost as easily without flip flops, as long as you don't insist on doing things within a loop. I realize this isn't the general case, but sometimes they turn out that way, for instance: > The Perl code that needs to be duplicated in Ruby is: > while (<>) { > $in_header = 1 .. /^$/; > $in_body = /^$/ .. eof(); > } This can be done by something like this (borrowing from your modified code): File.open("filename") do |f| str = f.readlines m = /\n\n/.match str in_header = m.pre_match in_body = m.post_match end This is as short as your flip flop code, and in fact, I can eliminate 1 line. I also cooked up a little magic in an attempt to reproduce flip flops on my own. It, of course, doesn't work as well as real flip flops. Here's my code: module Kernel def between(a, b, &blk) within = false proc do |l| within = true if a === l blk.call l if within within = false if b === l end end end With this, the following two pieces of code produce identical output: lines.each { |l| puts l if l=~/BEGIN/ .. l=~/END/ } lines.each &between(/BEGIN/, /END/ { |l| puts l } Further, you can technically chain together #between calls to make nested flipflops, but it looks very ugly. It's not as general as flip flops, nested or otherwise, either, so I'll let this stew for a while and see if I can come up with any hideous code that makes homebrew flip flops look nice. As for the devil's advocate talking about Unix idioms as methods, I'd point out that I can define my own #grep and use it, so you'd have to look it up anyway. I cannot define my own Ruby syntax based on other languages. Just a little devil's advocate back at you. :) I, for one, don't have a strong opinion about whether flip flops should be in the language. From the perl code posted, it looks like that's where it's taken from. Matz is also considering taking out some other perlisms, I believe (like certain $ variables), so maybe this is something else he took from perl that he wishes he didn't. However, I'd note that taking this out isn't a catastrophe. Most languages don't have such an idiom, and they do fine. And, maybe we can even figure out something to replace it that isn't too bad from either side's perspective, given that Ruby is quite able to be modified by itself for things like this. Sorry for the long post. - Dan