# Looking for help implementing static variables

**URL:** <https://www.ruby-forum.com/t/looking-for-help-implementing-static-variables/113279>\
**Category:** Ruby\
**Created:** [August 29, 2007, 2:38pm UTC](https://www.ruby-forum.com/t/looking-for-help-implementing-static-variables/113279 "2007-08-29T14:38:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecc23a/32.png) [@rubyman](https://www.ruby-forum.com/u/rubyman)\
**Post date:** [August 29, 2007, 2:38pm UTC](https://www.ruby-forum.com/t/looking-for-help-implementing-static-variables/113279/1 "2007-08-29T14:38:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rubyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecc23a/32.png) [@rubyman](https://www.ruby-forum.com/u/rubyman)\
**Post date:** [August 29, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/looking-for-help-implementing-static-variables/113279/2 "2007-08-29T15:16:38Z")

</div>

> 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](http://www.ruby-doc.org/core/classes/Marshal.html)

Ronald

---

<div class="post-metadata">

**Author:** ![rubyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecc23a/32.png) [@rubyman](https://www.ruby-forum.com/u/rubyman)\
**Post date:** [August 29, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/looking-for-help-implementing-static-variables/113279/3 "2007-08-29T22:59:22Z")

</div>

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](http://redshift.sourceforge.net/preferences)
