Range of regexps?

A

Alexey Verkhovsky

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

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?

Alex
 
R

Robert Klemme

Alexey Verkhovsky said:
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
 
J

James Edward Gray II

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

Robert gave a great answer, so I'll just add that this syntax doesn't
seem to be well liked. I believe I've read in two places now that it
may be going away in the future and is to be avoided. Just FYI.

James Edward Gray II
 
P

Phil Tomson

Robert gave a great answer, so I'll just add that this syntax doesn't
seem to be well liked. I believe I've read in two places now that it
may be going away in the future and is to be avoided. Just FYI.

It's also known as the flip-flop operator. It was borrowed from Perl.
There was a long thread about this about six months ago, I think it was
called "Save the flip-flop op" or somesuch (I started it).

I will be sorry to see it go.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top