Matz suggested that the MatchData could have been passed to the block
for convenience, however it looks more like a necessity, as I don't see
a solution short of rewriting gsub.
This suggests that for 1.9, MatchData should be passed as an optional
second argument to the block. Yes?
Matz suggested that the MatchData could have been passed to the block
for convenience, however it looks more like a necessity, as I don't see
a solution short of rewriting gsub.
Here's a solution:
def meth(replace)
">x".gsub(%r!(\W)x!) { |match| replace[$~] }
end
replace = lambda { |match|
puts "match is #{match.inspect}"
puts "match[1] is #{match[1].inspect}"
}
Not really, it somehow the proc object that is evaluated "first"
def meth(replace)
">x".gsub(%r!(\W)x!)do
puts "$1 is #{$1.inspect}"
end
end
and
def meth(replace)
">x".gsub(%r!(\W)x!, &replace)
p $1
end
work as expected
You will clearly see what I mean with the following example
def meth(replace)
">x".gsub(%r!(\W)x!, &replace)
end
replace = lambda { |match|
puts "last_match is #{Regexp.last_match.inspect}"
}
which produces
last_match is #<MatchData "">
Interesting stuff
Robert
···
On Thu, Dec 11, 2008 at 12:18 AM, Mike Gold <mike.gold.4433@gmail.com> wrote:
Matz suggested that the MatchData could have been passed to the block
for convenience, however it looks more like a necessity, as I don't see
a solution short of rewriting gsub.
This suggests that for 1.9, MatchData should be passed as an optional
second argument to the block. Yes?
--
Posted via http://www.ruby-forum.com/\.
--
Il computer non è una macchina intelligente che aiuta le persone
stupide, anzi, è una macchina stupida che funziona solo nelle mani
delle persone intelligenti.
Computers are not smart to help stupid people, rather they are stupid
and will work only if taken care of by smart people.