[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Austin Ziegler

11/20/2003 3:22:00 PM

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 * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.11.20
* 10.18.43


4 Answers

ts

11/20/2003 3:31:00 PM

0

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


sabbyxtabby

11/22/2003 12:46:00 PM

0

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

last_permutation

11/17/2011 5:40:00 PM

0

On Nov 17, 12:04 pm, Joe Bruno <ajta...@att.net> wrote:
> On Nov 17, 8:58 am, last_permutat...@yahoo.com wrote:
>
>
>
>
>
>
>
>
>
> > On Nov 17, 10:14 am, John Manning <jrobe...@terra.com.br> wrote:
>
> > > FILE UNDER: 'Idiocracy' is Reality TV for the GOP
>
> > It all boils down to who has the best ZioNazi asslick potential.
>
> > It certainly gives them a "leg up" so to speak.
>
> > > Winning is the last thing on Herman Cain's mind
>
> > > -- There are several indications that Herman Cain isn't
> > > particularly interested in moving into 1600 Pennsylvania Avenue.
>
> > > He's not bothering to build the kind of organizations that are
> > > needed to get people to the polls.
>
> > > He's not bothering to campaign in early states.
>
> > > He's not bothering to do any of the things that turn buzz into votes.
>
> > > But then, why should he? Being president is a thankless job.
>
> > > Running for president as a GOP candidate? Just about the best
> > > paying job you can get with no experience, no knowledge and nothing
> > > but an overweight ego.
>
> > > Why shouldn't Cain "run for president" as a profile-elevating
> > > exercise?
>
> > > Sarah Palin earned an estimated $12 million in 2010 as a
> > > celebrity Republican woman.
>
> > > How many of last cycle's candidates for the GOP nod went on
> > > to television shows, radio platforms and big book contracts? Just
> > > about all of them—except the guy who "won."
>
> > > Where being a Democratic candidate can leave you working for years
> > > to fill in the financial hole, being a GOP candidate has proven to
> > > be an extremely lucrative profession.
>
> > > GOP voters have an insatiable appetite for being told what they
> > > already believe, with bonus points for covering it with a heap
> > > of psuedo-evangelicalese rhetoric.
>
> > > Herman Cain isn't running for president. He's running to be the
> > > next Sarah Palin. He's not competing with Barack Obama; he's
> > > battling for bandwidth against the Kardashians.
>
> > > Charges that he has engaged in outrageous behavior? If Rick
> > > Perry wasn't passing out the info, Cain would have to do it
> > > himself.
>
> > > Would you bet against some cable network tossing a few million to
> > > the Hermanator for a series focusing on his invaluable business
> > > insight as he tours around his pizza empire, croons the occasional
> > > tune (with celebrity guests) and gets down with The Little
> > > People?
>
> > > Would you bet that the negotiations aren't underway right now?
>
> > > Herman Cain doesn't have to worry about the general election, or
> > > his harassment coming back on him, or learning the president of
> > > Uz becky becky stan stan. The only thing he's worried about is that
> > > he might accidentally win the nomination. That would really hurt
> > > his plans.
>
> > >http://www.dailykos.com/story/2011/11/14/1036458/-Winning-i.......
>
> Jews are less than 3% of the population, fool.

But about 99% of the media, not to mention AIPAC, turd.

Joe Bruno

11/17/2011 9:37:00 PM

0

On Nov 17, 9:40 am, last_permutat...@yahoo.com wrote:
> On Nov 17, 12:04 pm, Joe Bruno <ajta...@att.net> wrote:
>
>
>
>
>
>
>
>
>
> > On Nov 17, 8:58 am, last_permutat...@yahoo.com wrote:
>
> > > On Nov 17, 10:14 am, John Manning <jrobe...@terra.com.br> wrote:
>
> > > > FILE UNDER: 'Idiocracy' is Reality TV for the GOP
>
> > > It all boils down to who has the best ZioNazi asslick potential.
>
> > > It certainly gives them a "leg up" so to speak.
>
> > > > Winning is the last thing on Herman Cain's mind
>
> > > > -- There are several indications that Herman Cain isn't
> > > > particularly interested in moving into 1600 Pennsylvania Avenue.
>
> > > > He's not bothering to build the kind of organizations that are
> > > > needed to get people to the polls.
>
> > > > He's not bothering to campaign in early states.
>
> > > > He's not bothering to do any of the things that turn buzz into votes.
>
> > > > But then, why should he? Being president is a thankless job.
>
> > > > Running for president as a GOP candidate? Just about the best
> > > > paying job you can get with no experience, no knowledge and nothing
> > > > but an overweight ego.
>
> > > > Why shouldn't Cain "run for president" as a profile-elevating
> > > > exercise?
>
> > > > Sarah Palin earned an estimated $12 million in 2010 as a
> > > > celebrity Republican woman.
>
> > > > How many of last cycle's candidates for the GOP nod went on
> > > > to television shows, radio platforms and big book contracts? Just
> > > > about all of them—except the guy who "won."
>
> > > > Where being a Democratic candidate can leave you working for years
> > > > to fill in the financial hole, being a GOP candidate has proven to
> > > > be an extremely lucrative profession.
>
> > > > GOP voters have an insatiable appetite for being told what they
> > > > already believe, with bonus points for covering it with a heap
> > > > of psuedo-evangelicalese rhetoric.
>
> > > > Herman Cain isn't running for president. He's running to be the
> > > > next Sarah Palin. He's not competing with Barack Obama; he's
> > > > battling for bandwidth against the Kardashians.
>
> > > > Charges that he has engaged in outrageous behavior? If Rick
> > > > Perry wasn't passing out the info, Cain would have to do it
> > > > himself.
>
> > > > Would you bet against some cable network tossing a few million to
> > > > the Hermanator for a series focusing on his invaluable business
> > > > insight as he tours around his pizza empire, croons the occasional
> > > > tune (with celebrity guests) and gets down with The Little
> > > > People?
>
> > > > Would you bet that the negotiations aren't underway right now?
>
> > > > Herman Cain doesn't have to worry about the general election, or
> > > > his harassment coming back on him, or learning the president of
> > > > Uz becky becky stan stan. The only thing he's worried about is that
> > > > he might accidentally win the nomination. That would really hurt
> > > > his plans.
>
> > > >http://www.dailykos.com/story/2011/11/14/1036458/-Winning-i......
>
> > Jews are less than 3% of the population, fool.
>
> But about 99% of the media, not to mention AIPAC, turd.

Prove the 99%.