iniparser

iniparser gem - read / parse INI configuration, setttings and data files into a hash

Usage - INI.load, INI.load_file

Use INI.load or INI.load_file. Example:

require 'iniparser'

text = <<TXT
# comment
; another comment

key1 = hello
key2 : hi!

[section1]
key3 = salut   # end of line comment here

[section2]
key4: hola
blank =
blank2:

[ http://example.com ]
title =  A rose is a rose is a rose, eh?
title2:  A rose is a rose is a rose, eh?   # comment here
; another one here
title3 = A rose is a rose is a rose, eh?
TXT

hash = INI.load( text )
pp hash

resulting in:

{"key1"=>"hello",
 "key2"=>"hi!",
 "section1"=>{"key3"=>"salut"},
 "section2"=>{"key4"=>"hola", "blank"=>"", "blank2"=>""},
 "http://example.com"=>
  {"title"=>"A rose is a rose is a rose, eh?",
   "title2"=>"A rose is a rose is a rose, eh?",
   "title3"=>"A rose is a rose is a rose, eh?"}}

to access use:

puts hash['key1']
# => 'hello'
puts hash['key2']
# => 'hi!'
puts hash['section1']['key3']
# => 'salut'
puts hash['section2']['key4']
# => 'hola'
puts hash['section2']['blank']
# => ''
puts hash['section2']['blank2']
# => ''      
puts hash['http://example.com']['title']
# => 'A rose is a rose is a rose, eh?'
puts hash['http://example.com']['title2']
# => 'A rose is a rose is a rose, eh?'
puts hash['http://example.com']['title3']
# => 'A rose is a rose is a rose, eh?'

License

The iniparser scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Post them to the wwwmake forum. Thanks!