# YAML file problem

**URL:** <https://www.ruby-forum.com/t/yaml-file-problem/116879>\
**Category:** Ruby\
**Created:** [October 10, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/yaml-file-problem/116879 "2007-10-10T18:46:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiorry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jiorry](https://www.ruby-forum.com/u/jiorry)\
**Post date:** [October 10, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/yaml-file-problem/116879/1 "2007-10-10T18:46:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jiorry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jiorry](https://www.ruby-forum.com/u/jiorry)\
**Post date:** [October 10, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/yaml-file-problem/116879/2 "2007-10-10T19:12:20Z")

</div>

On 10/10/07, Feng L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![jiorry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jiorry](https://www.ruby-forum.com/u/jiorry)\
**Post date:** [October 10, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/yaml-file-problem/116879/3 "2007-10-10T19:22:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jiorry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jiorry](https://www.ruby-forum.com/u/jiorry)\
**Post date:** [October 10, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/yaml-file-problem/116879/4 "2007-10-10T21:49:51Z")

</div>

Feng L. wrote:

> 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’ # [FSDB](http://redshift.sourceforge.net/fsdb)

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
