Hi, i am working on a application so neccessary persistent value assign
to variable. For example:
variable = “first value”
…
…
codes
variable = “second value”
codes
variable = “third value”
And i want to use when my application restart, variable value should be
latest value so variable value should be “third value”. How can i do
this?
If you want persistent storage you’ll need to output the data into a
file, and read the file at startup.
The usual method for this would be something like an internal database
(SQlite?), or using YAML files. If it’s something really simple you can
just create a text file somewhere it won’t be interfered with.
Am 18.06.2013 15:07, schrieb Ebru A.:
codes
variable = “third value”
And i want to use when my application restart, variable value should be
latest value so variable value should be “third value”. How can i do
this?
There are many possibilities: e.g. write to a file (as string),
convert into YAML and write to a file, use some database, …
Here a simple example using YAML::Store, where the data is stored
in the file ‘values.yml’:
require ‘yaml/store’
store = YAML::Store.new(‘values.yml’)
create variables with default values in local scope
value = 1
name = ‘Fred’
read old values if stored
store.transaction do
value = store[‘value’] || value
name = store[‘name’] || name
end
puts “old:”
p value
p name
puts
change variables
value += 1
name = name.reverse.capitalize
puts “new:”
p value
p name
store values
store.transaction do
store[‘value’] = value
store[‘name’] = name
end
I had seen yaml when i search with google. But just i didn’t want to use
yaml
I decided to write variable in a file, thanks 