[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

gsub variables

Steve V

4/11/2005 6:10:00 PM

Are the $1, $`, $&, etc... variables within a gsub block truely global, or
are they some type of special value local to the block? To be global would
be fine in a single user environment, but in a multi-user web environment it
seems like this could cause problems. Do I need to worry about multi-user
issues with gsub?

Thanks,
Steve




4 Answers

Glenn Parker

4/11/2005 6:38:00 PM

0

Steve V wrote:
> Are the $1, $`, $&, etc... variables within a gsub block truely global, or
> are they some type of special value local to the block?

They are actually per-thread variables, meaning they look like global
variables, but each thread maintains its own separate set of values for
them.

> To be global would
> be fine in a single user environment, but in a multi-user web environment it
> seems like this could cause problems. Do I need to worry about multi-user
> issues with gsub?

It depends on how you invoke Ruby. The only way I can see it being a
problem is if the state from the last Regexp operation during one
transaction were to leak into the next transaction. In other words, if
the very first thing a web transaction did was:

last_match = $&

Then maybe it could steal some information from an earlier transaction,
assuming that a long-lived thread handled multiple transactions. But if
every transaction is handled in a new thread, or in a new Ruby process,
then $& will always be nil to start with.

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


Tim Hunter

4/11/2005 6:39:00 PM

0

Steve V wrote:
> Are the $1, $`, $&, etc... variables within a gsub block truely global, or
> are they some type of special value local to the block? To be global would
> be fine in a single user environment, but in a multi-user web environment it
> seems like this could cause problems. Do I need to worry about multi-user
> issues with gsub?
>
> Thanks,
> Steve

According to the Pickaxe II, they're all local to the thread.

Christian Neukirchen

4/11/2005 6:45:00 PM

0

"Steve V" <ruby@digitalnothing.com> writes:

> Are the $1, $`, $&, etc... variables within a gsub block truely global, or
> are they some type of special value local to the block? To be global would
> be fine in a single user environment, but in a multi-user web environment it
> seems like this could cause problems. Do I need to worry about multi-user
> issues with gsub?
>
> Thanks,
> Steve

They are local to the thread.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Steve V

4/11/2005 9:44:00 PM

0

> > Are the $1, $`, $&, etc... variables within a gsub block truely
> global, or
> > are they some type of special value local to the block?
>
> They are actually per-thread variables, meaning they look like global
> variables, but each thread maintains its own separate set of values for
> them.
>

Thanks everyone for the quick response.

Steve