Hi, I just wanted some clarification on this.
In production mode, while I know that object instance variables are local to the object and so to the request, but is this same with class variables and class instance variables or are they shared between requests?
I have this question, because I know that classes are cached in production mode. But what does this mean? Like, classes are initialised and cached at the start of the server (and so are class variables and class level instance variables) and are persistent in the memory throughout, or it's like, just class definitions are cached (so they are not read every time from the disk), and they are re- initialised for every request, so that every request has its own set?
Say, in file `lib/example.rb` I have defined something like -
#lib/example.rb module Example class << self
def assign value @@class_variable = value @class_instance_variable = value end
def read [@@class_variable, @class_instance_variable] end
end end
#app/controllers/application_controller.rb class ApplicationController < ActionController::Base before_filter :initialise
def initialise Example.assign params[:name] end end
1. Request "A" comes with params[:name] as "a". 2. While request "A" executes, request "B" comes with params[:name] as "b". 3. Now after arrival of request "B", if code in request "A" accesses Example.read(), what would it return? ['b', 'b'], ['a', 'a'] or (maybe, just maybe) ['a', 'b']?
I'd also like to write one more example -
#lib/example.rb module Example class << self
@@class_variable = 'default' @class_instance_variable = 'default'
def assign value @@class_variable = @class_instance_variable = value end
def read [@@class_variable, @class_instance_variable] end end end
1. Request "A" comes, executes Example.assign('a') and finishes. 2. Request "B" comes and tries to execute Example.read(), without modifying the variables. What would it get? "default" or "a"?
PS: While writing, I felt like this is a silly question. I'm a novice, anyway.
- thanks.