Ask a Question related to Ruby, Design and Development.
-
David Garamond #1
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
-
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}... -
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. ... -
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... -
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 -
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*$/,... -
Yukihiro Matsumoto #2
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
-
Austin Ziegler #3
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:
Is there an easy way for String#gsub (et al.) to check the arity of> 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.
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
-
ts #4
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
-
Austin Ziegler #5
Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?
On Fri, 21 Nov 2003 00:31:05 +0900, ts wrote:
I had actually done what you did, Guy, but I don't know the C side of Ruby,> A> Is there an easy way for String#gsub (et al.) to check the arity of A>>>>>>> "A" == Austin Ziegler <austin@halostatue.ca> writes:
> the provided block?
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
-
Florian Gross #6
Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby1.9/ruby2?
Yukihiro Matsumoto wrote:
Moin!> Hi,
I like this, because matz told me I shouldn't be using $1 in the> |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.
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
-
Sabby and Tabby #7
Re: behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?
Austin Ziegler <austin@halostatue.ca> wrote:
Lightly tested, seems to work:> 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.
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



Reply With Quote

