From: Caleb Clausen Date: 2005-08-02T02:07:33+09:00 Subject: Re: Redefining Regexp#=== On 8/1/05, Pit Capitain wrote: > Hi, > > what's wrong with this code: > > class Regexp > alias :case_eq :=== > def ===( other ) > case_eq( other ) > end > end > > p( /x/ === "abxy" ) # => true > p( Regexp.last_match ) # => nil > p( /x/.case_eq( "abxy" ) ) # => true > p( Regexp.last_match ) # => # > > Why is Regexp.last_match not set when calling the redefined method? > This is ruby 1.8.2 (2004-12-25) [i386-mswin32] on Windows 2000. I have discovered that $1 and the like and Regexp.last_match behave differently than I expected. These all behave like local variables in the method that calls the Regexp matching method, even tho they look like global variables and a class method. Your === doesn't work the expected way because it's no longer the magical built-in version, which exports the local variables $1 and friends and Regexp.last_match to it's immediate caller. I don't think there's a way to workaround this behaviour. This is bad actually; it means there's no safe way to override Regexp#===, Regexp#match, etc safely, so that the callers still have all the same old behaviour. Regexp#last_match cannot be usefully overridden because you can't call the original method in the override to retrieve the MatchData... it ends up always nil. I really hope the current behavior can be changed..... I need to be able to override === and match safely. Can't these variables be made thread globals instead of locals? Or at least provide a way to re-export the matching local vars to your own caller? Or actually be able to overrride Regexp.last_match? Ps: why do you want this?