Ruby does not support natively reading and writing INI files. However, you can use a third-party iniparse gem library to read and write ini files.

It provides the IniParse.parse method to read the files. and to_ini method used to convert Ruby objects to Ini document structure.

Ini files are used to store configuration with key and value pairs. You can also nested or grouped keys and values into sections.

First, Install iniparse using the below command in Terminal.

~/file-extension-ruby-examples$ gem install iniparse
Fetching iniparse-1.5.0.gem
Successfully installed iniparse-1.5.0
Parsing documentation for iniparse-1.5.0
Installing ri documentation for iniparse-1.5.0
Done installing documentation for iniparse after 0 seconds
1 gem installed

Let’s define the ini file for the reading example.

; Application Configuration Settings

[Database]
; Database server address
server = localhost

; Database port number
port = 3306


; Admin username
username = admin

; Admin password (avoid including actual passwords in comments!)
password = secret

[feature]
is_ssl = true
twophase_commit =  true

How to read Ini file with Sections in Ruby

First, import the iniparse module into code using require. File.read(‘config.ini’): parses the config.ini file into memory using a variable. IniParse.parse() Parses the Ini file content and converts it into a Dictionary of Ruby Object config.

You can access individual keys and values using key and section

#{config['database']['hostname']}: get the value for a hostname key in the database section. #{config['hostname']}: You can directly call using key If there is no section.

require 'iniparse'

# Read INI file
config = IniParse.parse(File.read('config.ini'))

# Access settings
puts "Database Section:"
puts "hostname: #{config['database']['hostname']}"
puts "Port: #{config['database']['port']}"
puts "username: #{config['database']['username']}"

puts "\nFeature Section:"
puts "Ssl: #{config['feature']['is_ssl']}"
puts "twophase_commit: #{config['feature']['twophase_commit']}"

Write to INI file in Ruby

require 'iniparse'

# Create a new INI file in memory
configObject = IniParse::Document.new

# Create Headers and pairs configurations for it
configObject['employee'] = {'name' => 'Eric', 'id' => '1', 'salary' => '5000'}
configObject['department'] = {'name' => 'sales', 'id' => '1'}

# Write to the file
File.write('result.ini', configObject.to_ini)
  • Import iniparse into a code
  • Create a config object of IniParse Document using IniParse::Document.new. It is an in-memory object representation of the Ini Document format structure.
  • Next, Create a Section and adds a key and value pairs, add these object to a Config document.
  • use the to_ini() method on the config document, to convert to ini structure
  • Finally, use File.write() to write an object to file

How to update an INI file with key and values in Ruby

This is a mix of read-and-write examples and updated configuration files.

  • First Read ini file using File.read() method and create a config object
  • Update values to section and key-value
  • Write the result of configDocument.to_ini to file using File.write() method
require 'iniparse'

# Parse INI file for update
configDocument = IniParse.parse(File.read('config.ini'))

# Update key and value pairs
configDocument['database']['username'] = 'newusername'
configDocument['database']['password'] = 'newsecuritypassword'

# Update configurations to the file
File.write('result.ini', configDocument.to_ini)