Use of global variables...

I am using a global array to accumulate controller output as it is added to the display, eg, when the user adds more items to a list, I add them to the global.

However, something strange is going on when I then try and use that global array in a controller method. In a nutshell:

@selected = Array.new $shown.each { |thing| puts "->#{thing}<-"; @selected.push thing; }

@selected is the array I want to produce (for simplicity, in the example here it should be identical to $shown). When this runs, the puts statement output is correct, and .each iterates thru the array.

But debugging with the next line:

@selected.each { |x| puts "XXX:"+x }

@selected is nil; it is not even an Array now! There is output from WEBrick mentioning an "authenticity_token", altho it doesn't appear to be an error and I don't know if it is relevent, I have not been using this for long:

Processing MainController#bysize (for 127.0.0.1 at 2009-05-23 20:36:27) [POST]   Parameters: {"authenticity_token"=>"5KvBJEMB3I....

Anyone know why, and if there is a work around?

Mk 27 wrote:

I am using a global array to accumulate controller output as it is added to the display, eg, when the user adds more items to a list, I add them to the global.

Don't ever do that. Forget that globals exist. They cause problems with maintainability and (I think) concurrency.

In this case, you should probably be using a local or @instance variable in the controller.

Best,

Marnen Laibow-Koser wrote:

Don't ever do that. Forget that globals exist. They cause problems with maintainability and (I think) concurrency.

Yes, that is the CS Doctrine, and generally I agree.

However, it seems dysfunctional to me that it would not be possible (I promise you will never be asked to maintain my project ;). This could be some kind of ruby object issue, I am as new to that as I am to rails.

After all, if globals were *that* bad, I presume

$global

a syntax for them should not exist either?

Mk 27 wrote: [...]

After all, if globals were *that* bad, I presume

$global

a syntax for them should not exist either?

Don't presume that. Just because a language feature exists doesn't mean it's a good idea to use it.

(Globals have their uses in short scripts. But...really...don't ever use them in an app of any complexity, or *your* code will be the dysfunctional part.)

Best,

Mk 27 wrote:

Marnen Laibow-Koser wrote:

In this case, you should probably be using a local or @instance variable in the controller.

*There is no way to do that in rails*.

Well, if you're so sure of that, why are you asking for help? :slight_smile:

The database I am working with is not modifiable via the interface (there is no new or create method).

That is irrelevant.

I would use an instance variable (populated by the list method), but when I call another method referencing this variable, it is no longer populated:

[...]

Any suggestions?

Do I understand correctly that you'd like to cache a database query in the controller between requests? If so, perhaps a @@class variable is the way to go here. Or maybe Rails' built-in caching would do what you need.

Best,

Marnen Laibow-Koser wrote:

Mk 27 wrote:

Marnen Laibow-Koser wrote:

In this case, you should probably be using a local or @instance variable in the controller.

*There is no way to do that in rails*.

Well, if you're so sure of that, why are you asking for help? :slight_smile:

Because, as they say, it is like pulling teeth sometimes :slight_smile:

Actually, I didn't really believe that, but after doing a little more research, I learned that in a production environment a class variable will not work either, and that it is true:

*YOU CANNOT DO THAT IN RAILS*

ie, you will have to write that data to a file of some sort, and use some kind of per user key to identify it. Which sucks, but I guess that is life.

Do I understand correctly that you'd like to cache a database query in the controller between requests? If so, perhaps a @@class variable is the way to go here. Or maybe Rails' built-in caching would do what you need.

Thanks for ringing my head Marnen re: class variables. I only skimmed thru the pickaxe book on the web last week. My stupidity.

Perhaps this issue has been solved with the "built-in caching"? The aforementioned "research" was another, similar forum thread from 2006. Hopefully -- then I will have to admit

*YOU CAN DO THAT IN RAILS*

So thanks again -- MK

Not to be snarky, but this seems to describe a session variable perfectly...

--Matt Jones

I just googled "Ruby on Rails session variable" and found virtually no info, so I still don't know what this refers to. It does not come up in the searchable API either, but I imagine they are in there -- can anyone point me to the spot? Otherwise I will just have to wait for the books I requested at the library to roll in next week...

see the Session section at ActionController::Base

Fred