global (accessible anywhere), but per-request variable

If I understand it correctly it would be enough to initialize something global like a singleton, $global, whatever, with a filter in ApplicationController. If you need it outside the web app, e.g. in the console, it could have some default set in environment.rb.

-- fxn

Hi Jenda,

And I don't think initializing a $global would work. What if two
requests come at about the same time? Request A inicializes the $global,
request B inicialized the $global, and then requst A used the $global
using the level and other settings set by request B? No thanks!

I don't think that's going to be a problem.

Your web server might be able to process multiple requests
simultaneously, but that's probably because it's got multiple
dispatch.fcgi processes running. But each of those processes runs in
a strictly serial fashion. Rails doesn't somehow start processing the
next request while the previous one is still finishing, so don't worry
about that.

I know I can use a before:filter to get a snippet of code ran at the
start of the request processing, what I don't know is where to store the
stuff then so that it's global as "accessible from anywhere" but
request-specific.

Any module or class that's loaded is available throughout all your
models, views and controllers etc., so if you set a class variable on
a module or class it will be available anywhere; you can then clean it
up in an after-filter so it doesn't persist into the next request.

Regards,
Dave

Yeah, the assumption you miss is that Rails is single-thread multi-process. Two dynamic requests cannot be served at the same time by the same Ruby interpreter.

-- fxn