# Match data variables

**URL:** <https://rubytalk.org/t/match-data-variables/53842>\
**Category:** ruby-talk\
**Created:** [10 June 2009 15:03 UTC](https://rubytalk.org/t/match-data-variables/53842 "2009-06-10T15:03:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lars\_Christensen1](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Lars\_Christensen1](https://rubytalk.org/u/Lars_Christensen1)\
**Post date:** [10 June 2009 15:03 UTC](https://rubytalk.org/t/match-data-variables/53842/1 "2009-06-10T15:03:43Z")

</div>

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)  
&nbsp;&nbsp;gets =~ pattern or fail  
end

expect /(\d+)/

p $1 #=\> nil  
p $~ #=\> nil ☹

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

---

<div class="post-metadata">

**Author:** ![Matt\_Neuburg](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@Matt\_Neuburg](https://rubytalk.org/u/Matt_Neuburg)\
**Post date:** [10 June 2009 16:15 UTC](https://rubytalk.org/t/match-data-variables/53842/2 "2009-06-10T16:15:07Z")

</div>

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

def expect(str, pattern)  
&nbsp;&nbsp;str =~ pattern  
&nbsp;&nbsp;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)  
> > &nbsp;&nbsp;gets =~ pattern or fail  
> > end
> > 
> > expect /(\d+)/
> > 
> > p $1 #=\> nil  
> > p $~ #=\> nil ☹
> > 
> > I would like $~ to contain the match data here.

---

<div class="post-metadata">

**Author:** ![Charles\_Nutter](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/charles_nutter/32/2567_2.png) [@Charles\_Nutter](https://rubytalk.org/u/Charles_Nutter)\
**Post date:** [10 June 2009 18:44 UTC](https://rubytalk.org/t/match-data-variables/53842/3 "2009-06-10T18:44:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [10 June 2009 21:10 UTC](https://rubytalk.org/t/match-data-variables/53842/4 "2009-06-10T21:10:21Z")

</div>

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

&nbsp;&nbsp;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)  
> > &nbsp;&nbsp;gets =~ pattern or fail  
> > end
> > 
> > expect /(\d+)/
> > 
> > p $1 #=\> nil  
> > p $~ #=\> nil ☹
> > 
> > I would like $~ to contain the match data here.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [11 June 2009 13:35 UTC](https://rubytalk.org/t/match-data-variables/53842/5 "2009-06-11T13:35:52Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 June 2009 13:47 UTC](https://rubytalk.org/t/match-data-variables/53842/6 "2009-06-11T13:47:24Z")

</div>

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/](http://blog.rubybestpractices.com/)
