gsub variables

S

Steve V

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
 
G

Glenn Parker

Steve said:
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.
 
T

Tim Hunter

Steve said:
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.
 
S

Steve V

Are the $1, $`, $&, etc... variables within a gsub block truely
global, or

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top