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
http://www.marnen.org
[email protected]

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?

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. The database I am working with
is not modifiable via the interface (there is no new or create method).
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:

class SomeController
def list
@array = Some.find(:all)
end
def another_method
[don’t bother referring to @array, it is empty]
end
end

Since there are actually a variety of methods here that could have been
responsible for the last @array, I cannot simply repeat the block in
list and hope that @array will be what is it currently is/was when last
applied in a view.

Any suggestions?

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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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
http://www.marnen.org
[email protected]

On May 24, 8:18 am, Mk 27 [email protected] wrote:

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.

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

–Matt J.

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

Matt J. wrote:

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

–Matt J.

Well thanks Matt for waiting until I had spent the afternoon re-writing
my code to use instance variables (just kidding).

Actually, having no professional web dev experience, it occurred to me
that maybe this is what cookies would be good for*. At any rate, I was
probably jumping the gun a little (or getting derailed, haha) in my
ambitions for learning project #1.

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…

thanks again and in advance, MK

*Page caching is definitely not the thing. I need to keep track of what
has happened on the page – ie, all the relevant data is there in front
of the user. KEY QUESTION: Where would I start if I wanted to grab and
parse the “raw” page in it’s current state?

I was also thinking I could keep a hidden form, but the former (parse
the page) would be preferable if possible.

On May 24, 10:11 pm, Mk 27 [email protected] wrote:

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

Fred

Matt J. wrote:

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

–Matt J.

Yep, that’s exactly what I wanted, the equivalent of a global hash.
Evidently this is implemented with cookies.

Long live snarky!

Thanks Fred too for the reference.