From: Ronald Fischer Date: 2007-08-29T22:15:47+09:00 Subject: Re: Looking for help implementing static variables > 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 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