Match data variables

L

Lars Christensen

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 :-(

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

Matt Neuburg

Lars Christensen said:
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 :-(

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.
 
C

Charles Oliver Nutter

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
 
R

Robert Klemme

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 :-(

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
 
A

Albert Schlef

Robert said:
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.)
 
R

Robert Klemme

2009/6/11 Albert Schlef said:
Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top