Range of regexps?

Discussion in 'Ruby' started by Alexey Verkhovsky, Sep 29, 2004.

  1. 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
    Alexey Verkhovsky, Sep 29, 2004
    #1
    1. Advertising

  2. "Alexey Verkhovsky" <> schrieb im Newsbeitrag
    news:...
    > 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
    Robert Klemme, Sep 29, 2004
    #2
    1. Advertising

  3. 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/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
    James Edward Gray II, Sep 29, 2004
    #3
  4. Alexey Verkhovsky

    Phil Tomson Guest

    In article <>,
    James Edward Gray II <> 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/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.
    >


    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
    Phil Tomson, Sep 29, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    967
    Antoon Pardon
    Jul 25, 2006
  2. Lambda
    Replies:
    2
    Views:
    393
    James Kanze
    Jul 16, 2008
  3. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    151
    Tomoyuki Kosimizu
    Nov 27, 2003
  4. David Bird
    Replies:
    1
    Views:
    209
    Tiago Macedo
    Jun 23, 2008
  5. Joey Zhou
    Replies:
    5
    Views:
    234
    Joey Zhou
    Apr 15, 2011
Loading...

Share This Page