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.rubybestpractices.com/