Forum: Ruby on Rails global variable - @@var

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rw Y. (Guest)
on 2006-04-04 03:52
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?
Rw Y. (Guest)
on 2006-04-04 06:03
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?
Pete Y. (Guest)
on 2006-04-04 06:13
(Received via mailing list)
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.
Phillip H. (Guest)
on 2006-04-04 06:49
(Received via mailing list)
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/
Keith L. (Guest)
on 2006-04-04 08:29
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
Rw Y. (Guest)
on 2006-04-04 09:26
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?
Phillip H. (Guest)
on 2006-04-04 10:09
(Received via mailing list)
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.
Julian 'Julik' Tarkhanov (Guest)
on 2006-04-05 00:12
(Received via mailing list)
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
This topic is locked and can not be replied to.