Say I've got:
get '/' do
$random = Random.rand()
response.body = $random
end
If I have thousands of requests per second coming to /, will the $random be shared and 'leak' outside the context or will it act like a 'local' variable to the get block?
I imagine if it was defined outside of the context of get '/' do
it would indeed be shared but I wonder if there's a mechanic to $ in ruby that I'm not aware of.
Other than some very specific exceptions (such as the regular expression match related globals) a global is shared with everything in the process - there is no scoping.
This part of the Sinatra README about scope is always helpful to read but if you only need the variable to persist for the request then I think there are 3 main ways I'd suggest going about this, and really the key is a filter
A before block
@my_log
will go out of scope at the end of the request and be re-initialised at the beginning of the next one. It will be accessible by any route, so if for example you usedpass
to pass it on to another route that would be the only time the other blocks could see what had been set by the prior route block.Using the settings helper
Then same as above, just replace
@my_log
withsettings.my_log
. Without thebefore
block reinitialising it then the contents of@my_log
would be persisted across requests.Using the settings helper with something like Redis
Now the redis instance is available via
settings.redis
. No need to worry about variable scope (I'd use locals with it), just push straight to Redis. You get the best of both worlds then, but if you want you could do: