[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Range of regexps?

Alexey Verkhovsky

9/29/2004 10:47:00 AM

Have a question (as usual, from a thread on ruby-forum.org,
http://www.ruby-forum.org/bb/viewtopi...) :

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



3 Answers

Robert Klemme

9/29/2004 12:40:00 PM

0


"Alexey Verkhovsky" <alex@verk.info> 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/viewtopi...) :
>
> 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


James Gray

9/29/2004 1:18:00 PM

0

On Sep 29, 2004, at 5:47 AM, Alexey Verkhovsky wrote:

> Have a question (as usual, from a thread on ruby-forum.org,
> http://www.ruby-forum.org/bb/viewtopi...) :
>
> 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



ptkwt

9/29/2004 7:13:00 PM

0

In article <EF8415DB-1219-11D9-8E07-000A95BA45F8@grayproductions.net>,
James Edward Gray II <james@grayproductions.net> wrote:
>On Sep 29, 2004, at 5:47 AM, Alexey Verkhovsky wrote:
>
>> Have a question (as usual, from a thread on ruby-forum.org,
>> http://www.ruby-forum.org/bb/viewtopi...) :
>>
>> 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.
>

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