[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Match data variables

Lars Christensen

6/10/2009 3:04:00 PM

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.

5 Answers

matt_neuburg

6/10/2009 4:08:00 PM

0

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

Charles O Nutter

6/10/2009 6:45:00 PM

0

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?

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

Robert Klemme

6/10/2009 9:08:00 PM

0

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

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

Albert Schlef

6/11/2009 1:36:00 PM

0

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

Robert Klemme

6/11/2009 1:47:00 PM

0

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?

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

Cheers

robert

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