From: Robert Klemme Date: 2004-09-29T21:44:19+09:00 Subject: Re: Range of regexps? "Alexey Verkhovsky" schrieb im Newsbeitrag news:1096368560.937.5.camel@localhost.localdomain... > Have a question (as usual, from a thread on ruby-forum.org, > http://www.ruby-forum.org/bb/viewtopic.php?t=75) : > > Expression /start/../end/ is invalid because Regexp doesn't have <=> > method, but this code is printed in Pickaxe: > > while gets > print if /start/../end/ > end > > with comment > QTE > As well as representing sequences, ranges may also be used as > conditional expressions. For example, the following code fragment prints > sets of lines from standard input, where the first line in each set > contains the word ``start'' and the last line the word ``end.'' > UNQTE > > As the author of the thread puts it, > QTE > What in the world is this program really supposed to do? Whatever I type > in at standard input it prints out. /start/../end/ just seems to be a > synonym for "true". I can't make sense of this example. Is it wrong? > UNQTE It once was the togglig boolean operator that used implicitely $_. You will notice a message similar to this if you use "ruby -w" (with warnings): range-test.rb:15: warning: range literal in condition The short form is equivalent to this verbose and much clearer form: while ( line = gets ) print line if /start/ =~ line .. /end/ =~ line end The ".." in conditionals ("if", "while", "unless", "until") is special as it stores a boolean flag that is set to true when the first condition matches and remains true until the second condition matches. > To which I would like to add: what in the world is the syntactic > difference between > /start/../end/ > > and > > if (/start/../end/) > > that the former is rejected, and the latter is accepted by the > interpreter? It could be the precedence of "if" and "..". Regards robert