From: matz@... (Yukihiro Matsumoto) Date: 2003-08-18T16:47:47+09:00 Subject: Re: Bug when rerouting String#gsub with a block using $1? Hi, In message "Re: Bug when rerouting String#gsub with a block using $1?" on 03/08/18, Mauricio Fern�ndez writes: | "hello world".gsub(/(\w+)/) { print $1; $1 }; print "\n" | |Is this just impossible to do in Ruby? In pure Ruby, yes. Ah, wait. If you don't need thread safety, you can do it as: "hello world".gsub(/(\w+)/) { print $1; $1 }; print "\n" class String alias :old_gsub :gsub def gsub(*args, &block) if block old_gsub(*args) { $match = $~ eval("$~ = $match", block) # the trick here. yield $& } else old_gsub(*args, &block) end end end "hello world".gsub(/(\w+)/) { print $1; $1 }; print "\n" matz.