From: Robert Dober Date: 2008-12-11T18:54:58+09:00 Subject: Re: lambda with $1 fails as gsub block On Thu, Dec 11, 2008 at 12:18 AM, Mike Gold wrote: > Apparently $1 is bound in a peculiar way: 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 # Interesting stuff Robert > > http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/e24a19e6ea75e5de/0211d0b304cbbad0?hl=en%C3%93d0b304cbbad0 > > 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. Umberto Eco