"Guillaume Marcais" <guslist / free.fr> schrieb im Newsbeitrag
news:1055774509.12403.53.camel / comp...
> On Mon, 2003-06-16 at 09:40, Michael Campbell wrote:
> > > A range operator with a regexp works like a flip flop (bistable
> > > multi
> > > vibrator or so).  The value of the internal flag (and thus the
> > > result of
> > > the evaluation) changes in this way: initiallly it's false.  If the
> > > first
> > > RE matches it's switched to true. It stays true as long as the
> > > second RE
> > > does not match.  If it does, the flag goes back to false.
> > >
> > > while( line = gets )
> > >   if /BEGIN:VCARD/ =~ line .. /END:VCARD/ =~ line
>
> I don't get this syntax. Isn't the last line equivalent to:
>   if (/BEGIN:VCARD/ =~ line) .. (/END:VCARD/ =~ line)

No, it's not.  Your code first evaluates the regexps and then creates an
integer range!  My code considered the whole thing and creates only a
single range instance.  I guess there's a little bit of magic in the
parsing (i.e. a range with regexp will be treated differently from another
range).

> which creates at each iteration a new range object, which knows nothing
> about previous lines or state of the switch.

That's exactly the reason why it is not the same. :-)

> [gus@comp tests]$ irb -v
> irb 0.7.4(01/05/08)
> [gus@comp tests]$ irb
> irb(main):001:0> r = (/BEGIN:VCARD/ =~ line) .. (/END:VCARD/ =~ line)
> NameError: undefined local variable or method `line' for
> #<Object:0x353ce0>
>         from (irb):1
> irb(main):002:0> line = "toto"
> "toto"
> irb(main):003:0> r = (/BEGIN:VCARD/ =~ line) .. (/END:VCARD/ =~ line)
> ArgumentError: bad value for range
>         from (irb):3
>
> Why does line 3 crashes? What's the difference with your code above,
> when inside the if statement?

It's not the if but the brackets.

> Your piece of code works as explained but I can't make sense out of it.
> Can you shed some light on this please?

Did that help?

    robert