Ask a Question related to Ruby, Design and Development.

  1. #1

    Default $& write-protected?

    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


    Simon Strandgaard Guest

  2. Similar Questions and Discussions

    1. Attempted to read or write protected memory
      I am attempting to convert a PDF document into a jpg image. I have the code in place and understand how it works: Dim a As New Acrobat.AcroPDDoc()...
    2. C3 with pwd protected site
      I am using c3 on a mac and attempting to set it up with a windows server that a co worker has successfully accessed with c4 on windows. I am able to...
    3. write to database without giving write permission to IIS
      Hi there, I have some ASP code that writes to access database. For security reason I do not want IUSER to give write permission to database...
    4. re protected files
      does anybody know how to remove a protected file called lorraine its a pain it corupts my pc so i cant use it for an hour plz help thanx
    5. Difference between "Protected WithEvents myClassName" And "Protected myClassName" ?
      Hello, what is the difference between a) Protected WithEvents myClassName b) Protected myClassName Thanks, Andreas
  3. #2

    Default Re: $& write-protected?

    >>>>> "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
    ts Guest

  4. #3

    Default Re: $& write-protected?

    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





    Simon Strandgaard Guest

  5. #4

    Default Re: $& write-protected?

    >>>>> "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
    ts Guest

  6. #5

    Default Re: $& write-protected?

    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
    Simon Strandgaard Guest

  7. #6

    Default Re: $& write-protected?

    >>>>> "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



    ts Guest

  8. #7

    Default Re: $& write-protected?

    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.


    Yukihiro Matsumoto Guest

  9. #8

    Default Re: $& write-protected?

    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
    Simon Strandgaard Guest

  10. #9

    Default Re: $& write-protected?

    >>>>> "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




    ts Guest

  11. #10

    Default Re: $& write-protected?

    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 Guest

  12. #11

    Default Re: $& write-protected?

    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>
    Simon Strandgaard Guest

  13. #12

    Default Re: $& write-protected?

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

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

    see the struct RMatch in re.h. str is the string, regs are the
    registers. ruby use register to retrieve $&, $1, ...

    see for example rb_reg_match_pre() ($'), rb_reg_match_post() ($`),
    rb_reg_nth_match() ($&, $1, ...)

    If you change the struct RMatch, this change will be reflected on $&,
    $1, ...


    Guy Decoux


    ts Guest

  14. #13

    Default Re: $& write-protected?

    From: [email]matz@ruby-lang.org[/email] [mailto:matz@ruby-lang.org]
    > 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.
    Wow, this solves a problem I was dealing with. I wanted to implement "sub"
    on a array-like object such that list.sub(...) would be the same a calling
    sub on each element of the list. The problem was that list.sub(/re/) { $1 }
    would always give nil for the value of $1.

    Here's my solution.

    # -----------------------------------------------
    class FileList < Array
    class << self
    attr_accessor :match_data
    def set_match_data(md, block)
    FileList.match_data = md
    eval "$~ = FileList.match_data", block
    end
    end
    def sub(re, &block)
    collect do |item|
    item.sub(re) {
    FileList.set_match_data($~, block)
    block.call
    }
    end
    end
    end

    f = FileList.new
    f << "a.c" << "b.c"
    p f.sub(/^(.*)\.c$/) { "#{$1}.o" }
    # ---------------------------------------------

    The above solution is not thread safe, but that can be easily solved by
    using a mutex in set_match_data. Comments welcome.

    --
    -- Jim Weirich / Compuware
    -- FWP Capture Services
    -- Phone: 859-386-8855

    Weirich, James Guest

  15. #14

    Default Re: $& write-protected?

    On Thu, 20 Nov 2003 00:25:59 +0900, ts wrote:
    >>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:
    >
    > S> My first thought were to extern 'last_match_getter' from 're.c' and
    > S> just supply my own last_match_setter. But then I noticed the function are
    > S> are 'static' (private).
    >
    > see the struct RMatch in re.h. str is the string, regs are the
    > registers. ruby use register to retrieve $&, $1, ...
    >
    > see for example rb_reg_match_pre() ($'), rb_reg_match_post() ($`),
    > rb_reg_nth_match() ($&, $1, ...)
    >
    > If you change the struct RMatch, this change will be reflected on $&,
    > $1, ...
    I bail out, maybe I will look at it tomorrow. I hoped it wouldn't be this
    complicated (im tired). I will follow the other approach and substitution
    all occurencies of $& and $1-$9 instead.

    Thanks for sharing you wisdom.


    BTW: Is there a reason that functions not found in Ruby's .H files, often
    has a 'static' tag, so thats impossible to extern them?

    Would it make any sense to remove all 'static' tags?

    --
    Simon Strandgaard
    Simon Strandgaard Guest

  16. #15

    Default Re: $& write-protected?

    > 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.
    Here, here!

    I was trying to add some features to the regex engine and couldn't for
    similar reasons. (I was trying to leverage the built-in syntax for
    regexes, but to match against tree-shaped structures... Don't ask.)

    I eventually gave up because I couldn't create MatchData.

    Ari


    Aredridel Guest

  17. #16

    Default Re: $& write-protected?

    "Simon Strandgaard" <none> wrote:
    > That will work. But I also need to so assignment to $1 - $9, $', $`, $+
    class MatchData
    def MatchData.[](pre, match, post, *captures)
    str = [pre, match, post] * ''
    match = Regexp.quote(match.dup)

    captures.collect! do |cap|
    c = Regexp.quote(cap)
    i = match.index(c) or raise ArgumentError,
    "Not a backreference: #{cap.inspect}"
    match[i + c.size, 0] = ')'
    match.slice!(0, i) + '('
    end

    str =~ /#{captures * '' + match}(?=.{#{post.size}}\z)/m
    $~
    end
    end

    $~ = MatchData[*%w/alpha beta charlie beta be e ta t a/]

    p [$`, $&, $', *$~.captures]
    Sabby and Tabby Guest

  18. #17

    Default Re: $& write-protected?

    On Sun, 23 Nov 2003 06:20:06 -0800, Sabby and Tabby wrote:
    > "Simon Strandgaard" <none> wrote:
    >
    >> That will work. But I also need to so assignment to $1 - $9, $', $`, $+
    >
    [snip fake MatchData class]
    > $~ = MatchData[*%w/alpha beta charlie beta be e ta t a/]
    >
    > p [$`, $&, $', *$~.captures]
    Weew, nice. I were starting to believe that this were simply impossible.
    Surprise of today. I am grateful for your help. Thanks.

    --
    Simon Strandgaard
    Simon Strandgaard Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139