How do I read/write an ini file in ruby. I have an ini file that I need to
- read
- change an entry
- write out to a different location
How would I do that in ruby? The documentation on this is bleak.
How do I read/write an ini file in ruby. I have an ini file that I need to
How would I do that in ruby? The documentation on this is bleak.
file = File.new("your.ini", "r")
while (line = file.gets)
puts "#{line}" #additionally make changes
end
file.close
If I understand correctly,
outFile = File.new('out.ini', 'w')
File.open('in.ini', 'r') do |inFile|
inFile.each_line do |line|
# foo is the entry you want to change, baz is its new value.
outFile.puts(line.sub(/foo=(.*)/, 'foo=baz'))
end
end
outFile.close
Note that when you use File.open
with a block, the file will automatically be closed when the block terminates.
I recently used ruby-inifile. Maybe it's overkill compared to the simple snippets here...