您好,我对使用 ruby 编辑 ini 文件很感兴趣。我的意思是我想阅读例如文件中的变量,更改其内容并将更改保存回文件。我还想以干净的 ruby-ish 方式执行编辑。有 this relative gem但据我了解它的功能,我无法打开文件、编辑其内容并保存回磁盘。我可以读取现有文件或创建新文件。它可以通过它的合并功能来完成吗?我不确定如何。
最佳答案
你说:
as I understand its functions I can't open a file, edit its contents and save back to disk.
你可以很容易地做到这一点:
require 'inifile'
# Open and read the file
ini = IniFile.load('my_file.ini')
# Read its current contents
puts ini['section1']['foo']
# Edit the contents
ini['section1']['foo'] = 'baz'
# Save it back to disk
# You don't need to provide the filename, it remembers the original name
ini.save
关于ruby - 使用 ruby 编辑 ini 文件,首选干净的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392613/