From: Rick DeNatale Date: 2007-10-11T02:11:51+09:00 Subject: Re: YAML file problem On 10/10/07, 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 > # > 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.denhaven2.com/