From: Charles Oliver Nutter Date: 2009-06-11T03:44:53+09:00 Subject: Re: Match data variables On Wed, Jun 10, 2009 at 10:03 AM, Lars Christensen wrote: > I understand that $~ and its friends ($&, $1, ...) are method local, but: > > Can a custom method mimic the behaviour of String#match, String#=~ etc > and set the match data for the caller? No. There is no way (without a supergross hack like using set_trace_func or something). This is one of my strongest arguments against those variables and methods that manipulate them, since they have this extra "magic" behavior that you can't mimic in Ruby code (without implementation-specific hacks). For this reason no methods that access/modify $~ or $_ should ever be wrapped or aliased, and we even warn against aliasing in JRuby since we use the presence of those methods to make some optimization decisions. I wish $~ and $_ were not in the language. - Charlie