Am 18.06.2013 15:07, schrieb Ebru Akagunduz:
> 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?

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



-- 
<https://github.com/stomar/>