Should $1 propagate through method calls?

Hello all.

When running this snippet, I expected that, since $1 "looks" like a
global variables, it would be propagated across method calls. Should it
be?

def go
print 'got', $1
end

'a' =~ /(a)/
print 'got1', $1
go

expected output:

got1agota

actual output :
$ ruby -v test.rb
tcs-ruby 1.9.3p28 (2012-01-28, TCS patched 2012-01-30) [i386-mingw32]
got1agot

Jruby "sometimes does and sometimes doesn't" propagate this.

Is this expected? Which way is right?
Thanks.
-roger-

···

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

From the pickaxe, Variables and Constants:

$1 to $9

String

The contents of successive groups matched in a successful pattern match. In "cat" =~/(c|a)(t|z)/, $1 will be set to “a” and $2 to “t”. THIS VARIABLE IS LOCAL TO THE CURRENT SCOPE. [r/o, thread]

(emphasis mine)

···

On Jun 27, 2012, at 13:26 , Roger Pack wrote:

Hello all.

When running this snippet, I expected that, since $1 "looks" like a
global variables, it would be propagated across method calls. Should it
be?

Well that explains it. Thanks everybody.
-r

···

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

Personally I love the Perl heritage stuff like this. But for someone coming
from a non-Perl background, damn that must seem pretty weird and surprising.

···

On Wed, Jun 27, 2012 at 4:36 PM, Ryan Davis <ryand-ruby@zenspider.com>wrote:

The contents of successive groups matched in a successful pattern match.
In "cat" =~/(c|a)(t|z)/, $1 will be set to “a” and $2 to “t”. THIS VARIABLE
IS LOCAL TO THE CURRENT SCOPE. [r/o, thread]

--
Avdi