class Hashery::IniHash

Hash class with methods to read from and write into ini files.

A ini file is a text file in a specific format, it may include several fields which are sparated by field headlines which are enclosured by “[]”. Each field may include several key-value pairs.

Each key-value pair is represented by one line and the value is sparated from the key by a “=”.

Examples ↑

Example ini file ↑

# this is the first comment which will be saved in the comment attribute
mail=info@example.com
domain=example.com # this is a comment which will not be saved
[database]
db=example
user=john
passwd=very-secure
host=localhost
# this is another comment
[filepaths]
tmp=/tmp/example
lib=/home/john/projects/example/lib
htdocs=/home/john/projects/example/htdocs
[ texts ]
wellcome=Wellcome on my new website!
Website description = This is only a example. # and another comment

Example object ↑

Ini#comment stores:

"this is the first comment which will be saved in the comment attribute"

Ini’s internal hash stores:

{
 "mail" => "info@example.com",
 "domain" => "example.com",
 "database" => {
  "db" => "example",
  "user" => "john",
  "passwd" => "very-secure",
  "host" => "localhost"
 },
 "filepaths" => {
  "tmp" => "/tmp/example",
  "lib" => "/home/john/projects/example/lib",
  "htdocs" => "/home/john/projects/example/htdocs"
 }
 "texts" => {
  "wellcome" => "Wellcome on my new website!",
  "Website description" => "This is only a example."
 }
}

As you can see this module gets rid of all comments, linebreaks and unnecessary spaces at the beginning and the end of each field headline, key or value.

Using the object ↑

Using the object is stright forward:

ini = IniHash.new("path/settings.ini")
ini["mail"] = "info@example.com"
ini["filepaths"] = { "tmp" => "/tmp/example" }
ini.comment = "This is\na comment"
puts ini["filepaths"]["tmp"]
# => /tmp/example
ini.write()

Acknowlegements ↑

IniHash is based on ini.rb.

Copyright © 2007 Jeena Paradies <info@jeenaparadies.net>