Global variable - @@var


#1

I tried to keep track of the number of access to a specific controller.
I declared ‘@@mycount = 0’ in the controller class. The methods in the
controller will increment the @@mycount (@@mycount = @@mycount + 1) and
print the value to the html page and show in the browser. The value is
always 1, no matter how many times I access the controller and its
action(s). Is this not the right way to create and share a global
variable in memory?


#2

I’ll bet that it’s because, when Rails is running in development
mode, the controller class gets reloaded on each request, thereby
overwriting the value of any class variables.


#3

I moved the global variables to a new class defined inside
environment.rb and get around the issue. However, I am still curious
why the global variables can’t be defined in controller class (and I
suspect it won’t work in model class as well).

rw y. wrote:

I tried to keep track of the number of access to a specific controller.
I declared ‘@@mycount = 0’ in the controller class. The methods in the
controller will increment the @@mycount (@@mycount = @@mycount + 1) and
print the value to the html page and show in the browser. The value is
always 1, no matter how many times I access the controller and its
action(s). Is this not the right way to create and share a global
variable in memory?


#4

Phillip H. wrote:

On 4/04/2006, at 2:03 PM, rw y. wrote:

I moved the global variables to a new class defined inside
environment.rb and get around the issue. However, I am still curious
why the global variables can’t be defined in controller class (and I
suspect it won’t work in model class as well).

Controllers and Models don’t persist, they’re killed off at the end
of each request. Models are stateful, the rest of Rails is stateless,
excepting sessions. If you want to store data use a session or a model.


Phillip H.
removed_email_address@domain.invalid
http://www.sitharus.com/

The session is indeed the way to go - and is easy:

session[:mycount] += 1

would increment the count, and you can retrieve it from any controller
or view.

hth,
Keith


#5

Keith L. wrote:

Phillip H. wrote:

On 4/04/2006, at 2:03 PM, rw y. wrote:

I moved the global variables to a new class defined inside
environment.rb and get around the issue. However, I am still curious
why the global variables can’t be defined in controller class (and I
suspect it won’t work in model class as well).

Controllers and Models don’t persist, they’re killed off at the end
of each request. Models are stateful, the rest of Rails is stateless,
excepting sessions. If you want to store data use a session or a model.


Phillip H.
removed_email_address@domain.invalid
http://www.sitharus.com/

The session is indeed the way to go - and is easy:

session[:mycount] += 1

would increment the count, and you can retrieve it from any controller
or view.

hth,
Keith

Thanks. But, wouldn’t session scope to only a specific client, not the
entire application space?


#6

On 4/04/2006, at 5:26 PM, rw y. wrote:

Thanks. But, wouldn’t session scope to only a specific client, not
the
entire application space?

There isn’t an application space. In some situations it may appear
that there is one, but don’t count on it. Running multiple instances
or clustered serving would all mess it up. Use the database, that’s
what it’s for.


#7

On 4/04/2006, at 2:03 PM, rw y. wrote:

I moved the global variables to a new class defined inside
environment.rb and get around the issue. However, I am still curious
why the global variables can’t be defined in controller class (and I
suspect it won’t work in model class as well).

Controllers and Models don’t persist, they’re killed off at the end
of each request. Models are stateful, the rest of Rails is stateless,
excepting sessions. If you want to store data use a session or a model.


Phillip H.
removed_email_address@domain.invalid
http://www.sitharus.com/


#8

On 4-apr-2006, at 1:52, rw y. wrote:

variable in memory?
Global variables are prefixed with $.

$counter += 1;

Note that counters are NOT shared between FCGI instances, so
essentially under any heavy use your global variables will be
unsynchronized across application instances.

As to your problem I think your controller class gets reloaded on
every request, so it’s class vars are getting reloaded.

Note that what youa re trying to achieve is bad practice.


Julian ‘Julik’ Tarkhanov
me at julik.nl