[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Looking for help implementing static variables

Ronald Fischer

8/29/2007 1:16:00 PM

> 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/Ma...

Ronald



5 Answers

Mô Tê Rang R?a

2/20/2011 9:57:00 PM

0

Not bad

On Feb 20, 12:52 pm, halong <cco...@netscape.net> wrote:
> On Feb 20, 1:22 pm, Mô Tê Rang R?a <moterang....@hotmail.com> wrote:
>
> > Cho*` xem coi ?
>
> > Hoa La`i ma` bung ra thi` hoa cu*'t lo*.n cu?a VN cu~ng nhi nho^ theo
>
> > Cho ma^'y tha(`ng Congsan qua('n de^' la.i
>
> > On Feb 20, 11:12 am, "Su'ngM16" <sung...@yahoo.com> wrote:
>
> > > Trung Co^.ng xua^'t kha^?u ga.o gia?, tru*'ng gia?, ......nhu*ng so*.
> > > nha^.p ...hoa la`i thu*' thie^.t (Jasmine Revolution)
>
> > >http://www.youtube.com/watch?v=c...
>
..> VN co' ca^u hoa la`i ma` ca('m bai~ ...
>
> phen na`y ngu+o+i` bie^?u ti`nh la` hoa la`i  va^.y thi` ma^'y tha(`ng
> CS thi` chi'nh la` nhu+~ng bai~ cu+'t no+.n ro^`i
>
> ;-)))))))))))))))))))

Su'ngM16

2/21/2011 2:52:00 AM

0

http://www.youtube.com/watch?v=B...



On Feb 20, 1:57 pm, Mô Tê Rang R?a <moterang...@hotmail.com> wrote:
> Not bad
>
> On Feb 20, 12:52 pm, halong <cco...@netscape.net> wrote:> On Feb 20, 1:22 pm, Mô Tê Rang R?a <moterang...@hotmail.com> wrote:
>
> > > Cho*` xem coi ?
>
> > > Hoa La`i ma` bung ra thi` hoa cu*'t lo*.n cu?a VN cu~ng nhi nho^ theo
>
> > > Cho ma^'y tha(`ng Congsan qua('n de^' la.i
>
> > > On Feb 20, 11:12 am, "Su'ngM16" <sung...@yahoo.com> wrote:
>
> > > > Trung Co^.ng xua^'t kha^?u ga.o gia?, tru*'ng gia?, ......nhu*ng so*.
> > > > nha^.p ...hoa la`i thu*' thie^.t (Jasmine Revolution)
>
> > > >http://www.youtube.com/watch?v=c...
>
> .> VN co' ca^u hoa la`i ma` ca('m bai~ ...
>
>
>
> > phen na`y ngu+o+i` bie^?u ti`nh la` hoa la`i  va^.y thi` ma^'y tha(`ng
> > CS thi` chi'nh la` nhu+~ng bai~ cu+'t no+.n ro^`i
>
> > ;-)))))))))))))))))))

Su'ngM16

2/21/2011 5:52:00 PM

0

Tu*` Thu*o*.ng Ha?i, Hoa La`i lan qua Ba('c Kinh :-)

http://www.youtube.com/watch?v=i...



On Feb 20, 6:51 pm, "Su'ngM16" <sung...@yahoo.com> wrote:
> http://www.youtube.com/watch?v=B...
>
> On Feb 20, 1:57 pm, Mô Tê Rang R?a <moterang....@hotmail.com> wrote:
>
> > Not bad
>
> > On Feb 20, 12:52 pm, halong <cco...@netscape.net> wrote:> On Feb 20, 1:22 pm, Mô Tê Rang R?a <moterang...@hotmail..com> wrote:
>
> > > > Cho*` xem coi ?
>
> > > > Hoa La`i ma` bung ra thi` hoa cu*'t lo*.n cu?a VN cu~ng nhi nho^ theo
>
> > > > Cho ma^'y tha(`ng Congsan qua('n de^' la.i
>
> > > > On Feb 20, 11:12 am, "Su'ngM16" <sung...@yahoo.com> wrote:
>
> > > > > Trung Co^.ng xua^'t kha^?u ga.o gia?, tru*'ng gia?, ......nhu*ng so*.
> > > > > nha^.p ...hoa la`i thu*' thie^.t (Jasmine Revolution)
>
> > > > >http://www.youtube.com/watch?v=c...
>
> > .> VN co' ca^u hoa la`i ma` ca('m bai~ ...
>
> > > phen na`y ngu+o+i` bie^?u ti`nh la` hoa la`i  va^.y thi` ma^'y tha(`ng
> > > CS thi` chi'nh la` nhu+~ng bai~ cu+'t no+.n ro^`i
>
> > > ;-)))))))))))))))))))

:))

2/21/2011 6:00:00 PM

0

DDo^`ng ba`o va` qua^n da^n ca'n chi'nh ha~y dde^` pho`ng Hotac ddang
qua?ng ca'o cho Ve.m va` la`m suy nhu*o*.c tinh tha^`n ca'ch ma.ng
cu?a toa`n da6n ddang so^i su.c.

-----------------------------------------------------------------------------------------------------------------------------------
On Mon, 21 Feb 2011 07:19:34 -0800 (PST), TrinhQuocThien
<trinhquocthien@gmail.com> wrote:

>Tha(`ng Hotac Ve.m na`y gia? ddo` dde^? qua?ng ca'o cho show na`y
>tre^n SCV !

>http://dantri1.vcmedia.vn/uploaded/guest/02.2011/trangtinh_sodomoi/...
>
>Ba` con ca^`n ca?nh gia'c tha(`ng Hotac Ve.m na`y.
>
>Muo^'n tao mua ve' ha? ? ;-)))))))))
>
>Co`n la^u nghen, tha(`ng Hotac Ve.m ! ;-))
>
>QTTT

------------------------------------------------------------------------------------------------------------------------------------
On Sun, 20 Feb 2011 18:51:35 -0800 (PST), "Su'ngM16"
<sungm16@yahoo.com> wrote:

>http://www.youtube.com/watch?v=B...
>
>
>
>On Feb 20, 1:57 pm, Mô Tê R?ng R?a <moterang...@hotmail.com> wrote:
>> Not bad
>>

Su'ngM16

2/21/2011 6:30:00 PM

0

Trung Qu?c c?m d?ch v? Twitter-Like To Stop The Spread Jasmine
Revolution c?a Dân ch?

http://giaitandangcongsan.blo...


On Feb 21, 10:00 am, ":))" <bennypo...@yahoo.com> wrote:
> DDo^`ng ba`o va` qua^n da^n ca'n chi'nh ha~y dde^` pho`ng Hotac ddang
> qua?ng ca'o cho Ve.m va` la`m suy nhu*o*.c tinh tha^`n ca'ch ma.ng
> cu?a toa`n da6n ddang so^i su.c.
>
> -----------------------------------------------------------------------------------------------------------------------------------
> On Mon, 21 Feb 2011 07:19:34 -0800 (PST), TrinhQuocThien
>
> <trinhquocth...@gmail.com> wrote:
> >Tha(`ng Hotac Ve.m na`y gia? ddo` dde^? qua?ng ca'o cho show na`y
> >tre^n SCV !
> >http://dantri1.vcmedia.vn/uploaded/guest/02.2011/trangtinh_......
>
> >Ba` con ca^`n ca?nh gia'c tha(`ng Hotac Ve.m na`y.
>
> >Muo^'n tao mua ve' ha? ? ;-)))))))))
>
> >Co`n la^u nghen, tha(`ng Hotac Ve.m ! ;-))
>
> >QTTT
>
> ------------------------------------------------------------------------------------------------------------------------------------
> On Sun, 20 Feb 2011 18:51:35 -0800 (PST), "Su'ngM16"
>
> <sung...@yahoo.com> wrote:
> >http://www.youtube.com/watch?v=B...
>
> >On Feb 20, 1:57 pm, M T R?ng R?a <moterang...@hotmail.com> wrote:
> >> Not bad