Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
YAML file problem
Feng Luhan
10/10/2007 4:47:00 PM
I hava a yaml file as application config file. I use YAML::Store to
reset yaml key value, but it is not work. please help me!
y = YAML::Store.new( "#{RAILS_ROOT}/config_app/config.yml", :Indent => 2
)
y.transaction do
y['value'].key1 = 'aaa' # undefined method `[]' for
#<YAML::DomainType>
y['value'].key2 = 'bbb'
end
File.open('config.yml','a+') do |file|
yy = YAML::load(file)
puts yy.value['key1'] # key1 = 111
yy.value['key1'] = 'aaa' # config.yml have not changed.
end
--
Posted via
http://www.ruby-...
.
3 Answers
Rick DeNatale
10/10/2007 5:12:00 PM
0
On 10/10/07, Feng Luhan <jiorry@gmail.com> wrote:
> I hava a yaml file as application config file. I use YAML::Store to
> reset yaml key value, but it is not work. please help me!
>
> y = YAML::Store.new( "#{RAILS_ROOT}/config_app/config.yml", :Indent => 2
> )
> y.transaction do
> y['value'].key1 = 'aaa' # undefined method `[]' for
> #<YAML::DomainType>
> y['value'].key2 = 'bbb'
> end
>
> File.open('config.yml','a+') do |file|
> yy = YAML::load(file)
> puts yy.value['key1'] # key1 = 111
> yy.value['key1'] = 'aaa' # config.yml have not changed.
> end
Why should config.yml change? yy is just whatever object which got
marshalled in when you read the file. Try:
y = YAML::Store.new( "#{RAILS_ROOT}/config_app/config.yml", :Indent => 2)
y.transaction do
y['value'].key1 = 'aaa'
y['value'].key2 = 'bbb'
end
y.transaction do
puts y.value['key1'] # key1 = 111
y.value['key1'] = 'aaa'
end
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denh...
Feng Luhan
10/10/2007 5:23:00 PM
0
We already have a config.yal
key1: 111
key2: 222
Now, user want to change key1 to 'aaa'. We must update the config file.
originally it is store in the Database, but I think storing in the yaml
file is better.
--
Posted via
http://www.ruby-...
.
Joel VanderWerf
10/10/2007 7:49:00 PM
0
Feng Luhan wrote:
> I hava a yaml file as application config file. I use YAML::Store to
> reset yaml key value, but it is not work. please help me!
>
> y = YAML::Store.new( "#{RAILS_ROOT}/config_app/config.yml", :Indent => 2
> )
> y.transaction do
> y['value'].key1 = 'aaa' # undefined method `[]' for
> #<YAML::DomainType>
> y['value'].key2 = 'bbb'
> end
>
> File.open('config.yml','a+') do |file|
> yy = YAML::load(file)
> puts yy.value['key1'] # key1 = 111
> yy.value['key1'] = 'aaa' # config.yml have not changed.
> end
I don't know much about YAML::Store, but if it is a YAML version of
PStore, then I recommend using my FSDB library instead.
Here's an example:
require 'fsdb' #
http://redshift.sourceforg...
db = FSDB::Database.new("~/tmp")
# Tell FSDB to use YAML format when it sees ".yml" and ".yaml" suffixes.
# It is easy to extend the recognition rules to handle other formats.
db.formats = [FSDB::YAML_FORMAT] + db.formats
# Write (atomically) the initial data to the file.
db["config.yml"] = { "key1" => 111, "key2" => 222 }
# Enter a thread-safe and process-safe transaction to
# change the state of the file.
db.edit("config.yml") do |cfg|
cfg['key1'] = 'aaa'
cfg['key2'] = 'bbb'
end
# Check that it works.
puts File.read(File.expand_path("~/tmp/config.yml"))
__END__
Output:
---
key1: aaa
key2: bbb
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
YAML file problem
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password