behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

    String#gsub(pattern) {|m| ... }

    It really would be nice to get match data in 'm', but this would surely
    break _a lot_ of scripts. How about String#regsub, #regsub!, #resub,
    #resub! ? What do others think?

    --
    dave


    David Garamond Guest

  2. Similar Questions and Discussions

    1. behaviour change of String#gsub(pattern) {|m| ... } for ruby1.9/ruby2?
      Hi -- On Fri, 21 Nov 2003, Florian Gross wrote: I've also just noticed this: irb(main):001:0> "abc".gsub(/((x?)abc)/) {|n,m| p n, m}...
    2. ruby-talk: 80813 (Rite/Ruby2.0 & Ruby vs OCaml)
      Hope nobody finds this annoying. Somehow I missed this message when it was originally sent, but I thought my reply might be useful to sombody. ...
    3. Rite/Ruby2.0 & Ruby vs OCaml
      Hi All, I'm new here, and I hope I don't offend anyone by mentioning a different programming language here on Ruby-Talk. I have two questions...
    4. Bug when rerouting String#gsub with a block using $1?
      Moin! This code: class String alias :old_gsub :gsub def gsub(*args, &block) old_gsub(*args, &block) end end
    5. gsub(/\s*$/, "") doubling string
      Hello, I recently downloaded ruby 1.8.0 p3, (2003-06-23) , and tried it on some code that chopped trailing spaces from a string using gsub(/\s*$/,...
  3. #2

    Default Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

    Hi,

    In message "behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?"
    on 03/11/20, David Garamond <lists@zara.6.isreserved.com> writes:

    |String#gsub(pattern) {|m| ... }
    |
    |It really would be nice to get match data in 'm', but this would surely
    |break _a lot_ of scripts. How about String#regsub, #regsub!, #resub,
    |#resub! ? What do others think?

    Sounds nice. The only reason for the current behavior is that sub
    predates MatchData. But we have to define migration path.

    matz.


    Yukihiro Matsumoto Guest

  4. #3

    Default Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

    On Thu, 20 Nov 2003 20:57:58 +0900, Yukihiro Matsumoto wrote:
    > In message "behaviour change of String#gsub(pattern) {|m| ... }
    >| String#gsub(pattern) {|m| ... }
    >|
    >| It really would be nice to get match data in 'm', but this would
    >| surely break _a lot_ of scripts. How about String#regsub,
    >| #regsub!, #resub, #resub! ? What do others think?
    > Sounds nice. The only reason for the current behavior is that sub
    > predates MatchData. But we have to define migration path.
    Is there an easy way for String#gsub (et al.) to check the arity of
    the provided block?

    String#gsub(pattern) { |m, md| ... }
    yields match string and matchdata

    String#gsub(pattern) { |m| ... }
    yields match string only

    That would break the least amount of code.

    -austin
    --
    austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.11.20
    * 10.18.43


    Austin Ziegler Guest

  5. #4

    Default Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

    >>>>> "A" == Austin Ziegler <austin@halostatue.ca> writes:

    A> Is there an easy way for String#gsub (et al.) to check the arity of
    A> the provided block?

    svg% cat b.rb
    #!/usr/bin/ruby
    def a(&block)
    p block.arity
    end

    a {|x| }
    a {|x, y| }
    svg%

    svg% b.rb
    1
    2
    svg%


    Guy Decoux


    ts Guest

  6. #5

    Default Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

    On Fri, 21 Nov 2003 00:31:05 +0900, ts wrote:
    >>>>>> "A" == Austin Ziegler <austin@halostatue.ca> writes:
    > A> Is there an easy way for String#gsub (et al.) to check the arity of A>
    > the provided block?
    I had actually done what you did, Guy, but I don't know the C side of Ruby,
    so I don't know how easy it is.

    I think that this is probably a good stab at a solution, though.

    -austin
    --
    austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.11.20
    * 11.31.12



    Austin Ziegler Guest

  7. #6

    Default Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby1.9/ruby2?

    Yukihiro Matsumoto wrote:
    > Hi,
    Moin!
    > |String#gsub(pattern) {|m| ... }
    > |
    > |It really would be nice to get match data in 'm', but this would surely
    > |break _a lot_ of scripts. How about String#regsub, #regsub!, #resub,
    > |#resub! ? What do others think?
    >
    > Sounds nice. The only reason for the current behavior is that sub
    > predates MatchData. But we have to define migration path.
    I like this, because matz told me I shouldn't be using $1 in the
    gsub-block and there's no way to not do so right now. :)

    Add a MatchData#to_str and it should work for most cases. We could give
    a warning for cases where scripts would still be broken by this chance.

    Regards,
    Florian Gross



    Florian Gross Guest

  8. #7

    Default Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

    Austin Ziegler <austin@halostatue.ca> wrote:
    > String#gsub(pattern) { |m, md| ... }
    > yields match string and matchdata
    >
    > String#gsub(pattern) { |m| ... }
    > yields match string only
    >
    > That would break the least amount of code.
    Lightly tested, seems to work:

    class String
    alias _sub sub
    alias _sub! sub!

    def sub(*a, &b)
    b ? _sub(*a) {|s| b.arity == 2 ? b[s, $~] : b[s]} : _sub(*a)
    end

    def sub!(*a, &b)
    s = sub(*a, &b)
    s == self ? nil : replace(s)
    end
    end

    Not portable, fragile, sometimes works:

    module Kernel
    alias _sub sub
    alias _sub! sub!

    def sub(*a, &b)
    $a, $b, $c = a, b, ""

    set_trace_func(proc {|*x|
    if x[0] == 'return'
    eval("$c.replace($_.sub(*$a, &$b))", x[4])
    set_trace_func(nil)
    end
    })

    $c
    end

    def sub!(*a, &b)
    $a, $b = a, b
    r, w = IO.pipe

    fork or return set_trace_func(proc {|*x|
    w.puts(eval("$_.sub!(*$a, &$b).inspect", x[4]))
    exit!
    })

    Process.wait
    $c = eval(r.readline) or return

    set_trace_func(proc {|*x|
    if x[0] == 'return'
    eval("$_.replace($c)", x[4])
    set_trace_func(nil)
    end
    })

    $c
    end
    end
    Sabby and Tabby 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