Looking for help implementing static variables

Hello,

I am working on an application where we are trying to remember previous
values from an execution of a Ruby program. Basically, we have a screen
where a user can type an ‘X’ into either a ‘Yes’ or a ‘No’ box. We do
not
want them to type Xs into both boxes. So if the ‘Yes’ box has an X in
it,
and then the user types an ‘X’ into the ‘No’ box, we want to clear the
‘Yes’
box automatically. Our Ruby program runs each time the user tabs out of
a
field on the screen or presses the Enter key.

Apparantly, Ruby does not have static variables where I can store the
box
values and refer to them when the program runs again, if I could I would
setup state variables where I could compare the current state to the
previous state and see what has changed between executions and then take
appropriate action such as clearing a ‘Yes’ box because currently there
is
an X in the ‘No’ box.

Any help would be greatly appreciated,

Thanks!

Harry T.

box automatically. Our Ruby program runs each time the user
tabs out of a
field on the screen or presses the Enter key.

Apparantly, Ruby does not have static variables where I can
store the box
values and refer to them when the program runs again

Don’t know whether this is the best solution, but I usually
marshal them to a file when the program is shut down, and reread
it when it is started up again. For example (using global variables),
to save the value of $checkstate for next execution, I write

require ‘pp’

File.write(“config.rc”,“w”) { |fh|
fh.puts(’$checkstate=’+$checkstate.pretty_inspect) }

and read it back with

eval(File.read(“config.rc”)) # sets $checkstate to the saved value

If you are unhappy with the idea of using global variables, you can
do it like this:


File.write(“config.rc”,“w”) { |fh|
fh.puts(‘CHECKSTATE=’+checkstate.pretty_inspect) }

(note the usage of upper case to denote a constant) and read it back
like
this:

eval(“module RC\n”+File.read(“config.rc”)+"\nend")
checkstate=RC::CHECKSTATE # sets checkstate to the saved value

Another possibility, suggested already in the list - but one which
I have not used myself yet (though I would like to try it out on
the next occasion) - is to use the Marshal module. See

http://www.ruby-doc.org/core/classes/Marshal.html

Ronald

Harry T. wrote:

Hello,

I am working on an application where we are trying to remember previous
values from an execution of a Ruby program. Basically, we have a screen
where a user can type an ‘X’ into either a ‘Yes’ or a ‘No’ box. We do not
want them to type Xs into both boxes. So if the ‘Yes’ box has an X in it,
and then the user types an ‘X’ into the ‘No’ box, we want to clear the ‘Yes’
box automatically. Our Ruby program runs each time the user tabs out of a
field on the screen or presses the Enter key.

This isn’t built into ruby, which is probably a good thing because there
are many different ways of doing it.

A typical solution will involve one of ruby’s standard object
persistence mechanisms, either Marshal (not humanly readable, but
efficient) or YAML (humanly readable, and also readable by Perl, Python,
etc.).

There’s also the question of where to store this file. On unix/linux,
you might want to use a “dot” file in the user’s home dir, or in
ENV[“HOME”]. On windows, there is a different standard for selecting the
location (ENV[‘APPDATA’], for example)).

I wrote a small library that does this using YAML. See:

http://redshift.sourceforge.net/preferences

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs