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.

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.

路路路

Lars Christensen <larschbelunktumdk@gmail.com> 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?

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.

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 Wed, Jun 10, 2009 at 10:03 AM, Lars Christensen<larschbelunktumdk@gmail.com> 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?

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

路路路

On 10.06.2009 17:03, 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?

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme 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.)

路路路

--
Posted via http://www.ruby-forum.com/\.

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

Cheers

robert

路路路

2009/6/11 Albert Schlef <albertschlef@gmail.com>:

Robert Klemme 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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/