From: Brendan Baldwin Date: 2006-07-19T07:13:10+09:00 Subject: How to preserve $~ when extending Regexp#match Consider the following Regexp/MatchData extension, which pops a copy of the original Regexp into the MatchData object resulting from Regexp#match. Forget for a minute *why* anyone might want to do this: class Regexp alias_method :original_match, :match def match(str) m=self.original_match(str) m.regexp=self unless m.nil? m end end class MatchData attr_accessor :regexp end Because of the magical nature of the $~ global variable and its derivatives $&,$+,$`,$',$1-9, we wind up losing it from scope with this extension to the Regexp class: /myregexp/.match("test my regexp") $~ # <= nil Is anyone out there aware of any way to set $~ in the "caller's" binding from within Regexp#match *without* requiring a manual call_stack to be implemented via set_trace_func? Thanks! --Brendan Baldwin :: ruby-forum@brendanbaldwin.com -- Posted via http://www.ruby-forum.com/.