Class | Ini |
In: |
lib/more/facets/ini.rb
|
Parent: | Object |
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 "=".
# 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
A Ini#comment stores: "this is the first comment which will be saved in the comment attribute" A Ini object 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 is stright forward:
ini = Ini.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()
comment | [RW] | :inihash is a hash which holds all ini data :comment is a string which holds the comments on the top of the file |
inihash | [RW] | :inihash is a hash which holds all ini data :comment is a string which holds the comments on the top of the file |
Reading comments from file
path is a path to the ini file
Returns a string with comments from the beginning of the ini file.
Reading data from file
path is a path to the ini file
returns a hash which represents the data from the file
Turn a hash (up to 2 levels deepness) into a ini string
inihash is a hash representing the ini File. Default is a empty hash.
Returns a string in the ini file format.
Writing a ini hash into a file
path is a path to the ini file inihash is a hash representing the ini File. Default is a empty hash. comment is a string with comments which appear on the
top of the file. Each line will get a "#" before. Default is no comment.