要編寫文件,須要打開YAML文件並寫入句柄。 require 'yaml' config = YAML.load_file('data.yml') puts config['last_update'] #in my file this is set to "some data" config['last_update'] = "other data" File.open('data.yml','w') do |h| h.write config.to_yaml end 輸出是 orcus:~ user$ ruby test.rb some data orcus:~ user$ cat data.yml --- last_update: other data