I assume there are answers to the obvious questions (Why not
Regexp#match? Why not 1.8.x?) so I won't ask them
I've tried,
unless String::respond_to?(:match)
(Is the :: thing only used by gov't employees?
I think you'd want:
unless String.instance_methods.include?("match")
class String
def match(regex)
regex.match(self)
end
end
but it doesn't set the group match variables, e.g.,
$1, $2, and so forth.
They'll get set inside the method, but not beyond. I think the
rationale for this is that otherwise you'd have no sane way of knowing
whether and when they were getting set:
re = /(blah)/
re.match(string)
some_method(whatever)
# did some_method do a match operation? did it reset the
# capture variables?
Of course, you can also use Florian's Binding.of_caller to get the environment
of the caller and substitute it in the eval, but callcc is expensive [even my
my variant, which doesn't use it, is quite expensive and you can only have a
trace_func at a time...].
···
On Wed, Nov 09, 2005 at 04:02:12AM +0900, Bil.Kleb@gmail.com wrote:
I got the '::' from the IO.read that ts gave me.
So, then my question becomes, can I create
a shim for 1.8's String#match with pure Ruby?