IniParse Build Status

IniParse is a pure Ruby library for parsing INI configuration and data files.

Main features

If you don't need the above mentioned features, you may find the simpler IniFile gem does all you need.

Opening an INI file

Parsing an INI file is fairly simple:

IniParse.parse( File.read('path/to/my/file.ini') ) # => IniParse::Document

IniParse.parse returns an IniParse::Document instance which represents the passed “INI string”. Assuming you know the structure of the document, you can access the sections in the INI document with IniParse::Document#[]. For example:

document = IniParse.parse( File.read('path/to/my/file.ini') )

document['a_section']
  # => IniParse::Lines::Section

document['a_section']['an_option']
  # => "a value"
document['a_section']['another_option']
  # => "another value"

In the event that duplicate options were found, an array of the values will be supplied to you.

document = IniParse.parse <<-EOS
  [my_section]
  key = value
  key = another value
  key = a third value
EOS

document['my_section']['key']
  # => ['value', 'another value', 'third value']

Options which appear before the first section will be added to a section called “__anonymous__”.

document = IniParse.parse <<-EOS
  driver = true

  [driver]
  key = value
EOS

document['__anonymous__']['driver']
  # => true

document['driver']['key']
  # => 'value'

Updating an INI file

document['a_section']['an_option']
  # => "a value"
document['a_section']['an_option'] = 'a new value'
document['a_section']['an_option']
  # => "a new value"

Delete entire sections by calling `#delete` with the section name…

document.delete('a_section')

… or a single option with `#delete` on the section object:

document['a_section'].delete('an_option')

Creating a new INI file

See IniParse::Generator.