Match data variables

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?

def expect(pattern)
gets =~ pattern or fail
end

expect /(\d+)/

p $1 #=> nil
p $~ #=> nil :frowning:

I would like $~ to contain the match data here.

Lars C. [email protected] wrote:

p $1 #=> nil
p $~ #=> nil :frowning:

I would like $~ to contain the match data here.

Regexp.last_match is a MatchData object and you can do whatever you like
with it. For example:

def expect(str, pattern)
str =~ pattern
Regexp.last_match
end
res = expect(“howdy”, /h/)
p res.begin(0)

You could even set some other global to Regexp.last_match, if you’re
in a “use globals” mood…

m.

On Wed, Jun 10, 2009 at 10:03 AM, Lars
Christensen[email protected] 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

On 10.06.2009 17:03, Lars C. wrote:

p $1 #=> nil
p $~ #=> nil :frowning:

I would like $~ to contain the match data here.

What stops you from

a) returning the MatchData instance from expect(), for example by using
#match
b) yielding the match data from expect to a block

? Why do you believe you need to somehow inject something into the
caller’s environment?

Kind regards

robert

Robert K. wrote:

What stops you from

[snip]

b) yielding the match data from expect to a block

Can the yielded block see the $1, $2, $3 vars?

(Sorry for not checking this out myself, my Ruby system is broken. BTW,
I’m not the original poster.)

2009/6/11 Albert S. [email protected]:

Robert K. wrote:

What stops you from

[snip]

b) yielding the match data from expect to a block

Can the yielded block see the $1, $2, $3 vars?

No but it would be easy to provide a MatchData instance or whatever
suits the use case best via block parameters.

Cheers

robert