On Fri, Feb 25, 2011 at 9:53 PM, 7stud -- <bbxx789_05ss / yahoo.com> wrote:
> Josh Cheek wrote in post #983791:
>> On Thu, Feb 24, 2011 at 8:04 PM, 7stud -- <bbxx789_05ss / yahoo.com>
>> wrote:
>>
>>>
>>> $1 is a *global* variable, so saying it doesn't persist outside of a
>>> block doesn't make any sense.
>>>
>>>
>> It isn't actually global. I don't know the specifics, but I used to
>> worry
>> about that too, and found out later it was not necessary.
>>
>> def meth_a
>> "a" =~ /(.)/
>> puts "in a, $1 = #{$1.inspect}"
>> end
>>
>> def meth_b
>> "b" =~ /(.)/
>> puts "in b, $1 = #{$1.inspect}"
>> meth_a
>> puts "in b, $1 = #{$1.inspect}"
>> end
>>
>> meth_b
>>
>>
>> # >> in b, $1 = "b"
>> # >> in a, $1 = "a"
>> # >> in b, $1 = "b"
>
> Uh oh.  ¨Βονεοξε ισ ηοιξτο θαφε το εψπμαιτθατ το νεδοεσ ξοαγ> like a regular global variable:

$1, $2 etc. look like global variables but they are in fact local to
the scope where they are set.  This automatically also means that they
are thread local.  Josh's example demonstrates that nicely.

The reason is to simplify applications which use multiple regular
expression matches - nested and concurrently.

Kind regards

robert

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