[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

$& write-protected?

Melanie Fielder

11/19/2003 9:24:00 AM

I am working on a regexp engine written entirely in Ruby.
I wish to test it against the rubicon testsuite, its using $& and $1-$9
heavily.

How do I assign my regexp-result to $& ?


irb(main):001:0> $& = "test"
SyntaxError: compile error
(irb):1: Can't set variable $&
from (irb):1
irb(main):002:0>

--
Simon Strandgaard


15 Answers

ts

11/19/2003 11:08:00 AM

0

>>>>> "S" == Simon Strandgaard <none> writes:

S> How do I assign my regexp-result to $& ?

$& is read-only

S> irb(main):001:0> $& = "test"

it depend what you want to do

/test/ =~ "test" # $& ==> "test"



--

Guy Decoux

Melanie Fielder

11/19/2003 12:25:00 PM

0

ts <decoux@moulon.inra.fr> skrev i en
nyhedsmeddelelse:rfcad6s62gg.fsf@moulon.inra.fr...
> >>>>> "S" == Simon Strandgaard <none> writes:
>
> S> How do I assign my regexp-result to $& ?
>
> $& is read-only
>
> S> irb(main):001:0> $& = "test"
>
> it depend what you want to do
>
> /test/ =~ "test" # $& ==> "test"

That will work. But I also need to so assignment to $1 - $9, $', $`, $+

Any ideas how to fake this?

Any rationale why they are write-protected?

--
Simon Strandgaard





ts

11/19/2003 12:49:00 PM

0

>>>>> "S" == Simon Strandgaard <none> writes:

S> Any ideas how to fake this?

Well, it's easy to do for $' and $`, no idea for the rest $1, ...

S> Any rationale why they are write-protected?

These variables are the result of a regexp against a string : it seems a
non sense to change this result.

--

Guy Decoux

Simon Strandgaard

11/19/2003 1:56:00 PM

0

On Wed, 19 Nov 2003 13:49:27 +0100, ts wrote:

>>>>>> "S" == Simon Strandgaard <none> writes:
>
> S> Any ideas how to fake this?
>
> Well, it's easy to do for $' and $`, no idea for the rest $1, ...
>
> S> Any rationale why they are write-protected?
>
> These variables are the result of a regexp against a string : it seems a
> non sense to change this result.

Aree, these values should only contain output from regexp.
But as a paradox I am developing my own regexp engine!

I want my regexp engine to be compatible with Ruby's, therefore I want to
exercise the engine with the Rubicon testsuite. But rubicon uses $& and
$1-$9. I can substitute all occurencies of $& and $1-$9 in rubicon.

If $& and $1-$9 assignment are impossible, then, then my regexp-engine
never will get compatible with Ruby's engine :-(

[RCR] remove write-protection of $&, $1-$9, $', $`, $+
or perhaps let them stay write-protected, but let assignment via const_set
become possible?

--
Simon Strandgaard

ts

11/19/2003 2:02:00 PM

0

>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:

S> I want my regexp engine to be compatible with Ruby's, therefore I want to
S> exercise the engine with the Rubicon testsuite. But rubicon uses $& and
S> $1-$9. I can substitute all occurencies of $& and $1-$9 in rubicon.

Be carefull with this test : it was designed for the *current* regexp
engine, this means, for example, that these tests must be modified for
Onigurama. Because Oniguruma will give different results.

S> [RCR] remove write-protection of $&, $1-$9, $', $`, $+

No need for this : just write an C extension which do this, and use this
extension *only* for testing your regexp engine.

Nobody, except you, need to modify these variables.


Guy Decoux



matz

11/19/2003 2:18:00 PM

0

Hi,

In message "Re: $& write-protected?"
on 03/11/19, Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:

|[RCR] remove write-protection of $&, $1-$9, $', $`, $+
|or perhaps let them stay write-protected, but let assignment via const_set
|become possible?

If you update $~ (the match result), $&, $1, etc. would follow
accordingly. So that the better RCR should be to create MatchData
without built-in regular expression match, I think.

matz.


Simon Strandgaard

11/19/2003 2:19:00 PM

0

On Wed, 19 Nov 2003 23:01:50 +0900, ts wrote:

>>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:
>
> S> I want my regexp engine to be compatible with Ruby's, therefore I want to
> S> exercise the engine with the Rubicon testsuite. But rubicon uses $& and
> S> $1-$9. I can substitute all occurencies of $& and $1-$9 in rubicon.
>
> Be carefull with this test : it was designed for the *current* regexp
> engine, this means, for example, that these tests must be modified for
> Onigurama. Because Oniguruma will give different results.

I have about 150 testcases which I both test with Ruby's current regexp
engine and against my own engine. So far my engine is fully compatible.

Perhaps I should also run the tests agains Oniguruma.
Do you know if Oniguruma can coexist with Ruby's current regexp engine?


> S> [RCR] remove write-protection of $&, $1-$9, $', $`, $+
>
> No need for this : just write an C extension which do this, and use this
> extension *only* for testing your regexp engine.
>
> Nobody, except you, need to modify these variables.

Great, so it *is* possible.. I will look at it tomorrow (C++SWIG).

--
Simon Strandgaard

ts

11/19/2003 2:24:00 PM

0

>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:

S> Perhaps I should also run the tests agains Oniguruma.

yes, like said previously, Oniguruma will give you different results.

S> Do you know if Oniguruma can coexist with Ruby's current regexp engine?

well, you can have 2 differents ruby : one with Oniguruma, the other with
the GNU regexp

S> Great, so it *is* possible.. I will look at it tomorrow (C++SWIG).
^^^^

no need of SWIG in this case :-)


Guy Decoux




Simon Strandgaard

11/19/2003 2:50:00 PM

0

On Wed, 19 Nov 2003 23:18:12 +0900, Yukihiro Matsumoto wrote:
> In message "Re: $& write-protected?"
> on 03/11/19, Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:
>
> |[RCR] remove write-protection of $&, $1-$9, $', $`, $+
> |or perhaps let them stay write-protected, but let assignment via const_set
> |become possible?
>
> If you update $~ (the match result), $&, $1, etc. would follow
> accordingly. So that the better RCR should be to create MatchData
> without built-in regular expression match, I think.

Good idea for an RCR. I vote yes immediately for a pure MatchData class ;-)

--
Simon Strandgaard


Simon Strandgaard

11/19/2003 3:19:00 PM

0

On Wed, 19 Nov 2003 23:24:17 +0900, ts wrote:

>>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:
> S> Great, so it *is* possible.. I will look at it tomorrow (C++SWIG).
> ^^^^
> no need of SWIG in this case :-)

Maybe overkill.

My first thought were to extern 'last_match_getter' from 're.c' and
just supply my own last_match_setter. But then I noticed the function are
are 'static' (private).

How do you think I shall implement this ?


server> gcc assign.c -I/usr/home/neoneye/install/ruby-1.8.1
/usr/lib/crt1.o: In function `_start':
/usr/lib/crt1.o(.text+0x81): undefined reference to `main'
/var/tmp//ccu9UXrr.o: In function `Init_Assign':
/var/tmp//ccu9UXrr.o(.text+0x17): undefined reference to `last_match_getter'
/var/tmp//ccu9UXrr.o(.text+0x21): undefined reference to `rb_define_virtual_variable'
server> expand -t4 assign.c
#include <ruby.h>

extern VALUE last_match_getter();

static void last_match_setter(VALUE val) {
/* TODO */
}

void Init_Assign() {
rb_define_virtual_variable("$&", last_match_getter, last_match_setter);
}
server>