Class | IniFile |
In: |
lib/inifile.rb
|
Parent: | Object |
This class represents the INI file and can be used to parse, modify, and write INI files.
VERSION | = | '2.0.2' |
encoding | [RW] | Get and set the encoding (Ruby 1.9) |
escape | [RW] | Enable or disable character escaping |
filename | [RW] | Get and set the filename |
Public: Open an INI file and load the contents.
filename - The name of the fiel as a String opts - The Hash of options (default: {})
:comment - String containing the comment character(s) :parameter - String used to separate parameter and value :encoding - Encoding String for reading / writing (Ruby 1.9) :escape - Boolean used to control character escaping :default - The String name of the default global section
Examples
IniFile.load('file.ini') #=> IniFile instance IniFile.load('does/not/exist.ini') #=> nil
Returns an IniFile intsnace or nil if the file could not be opened.
Public: Create a new INI file from the given content String which contains the INI file lines. If the content are omitted, then the :filename option is used to read in the content of the INI file. If neither the content for a filename is provided then an empty INI file is created.
content - The String containing the INI file contents opts - The Hash of options (default: {})
:comment - String containing the comment character(s) :parameter - String used to separate parameter and value :encoding - Encoding String for reading / writing (Ruby 1.9) :escape - Boolean used to control character escaping :default - The String name of the default global section :filename - The filename as a String
Examples
IniFile.new #=> an empty IniFile instance IniFile.new( "[global]\nfoo=bar" ) #=> an IniFile instance IniFile.new( :filename => 'file.ini', :encoding => 'UTF-8' ) #=> an IniFile instance IniFile.new( "[global]\nfoo=bar", :comment => '#' ) #=> an IniFile instance
Public: Get the section Hash by name. If the section does not exist, then it will be created.
section - The section name as a String.
Examples
inifile['global'] #=> global section Hash
Returns the Hash of parameter/value pairs for this section.
Public: Set the section to a hash of parameter/value pairs.
section - The section name as a String. value - The Hash of parameter/value pairs.
Examples
inifile['tenderloin'] = { 'gritty' => 'yes' } #=> { 'gritty' => 'yes' }
Returns the value Hash.
Public: Remove a section identified by name from the IniFile.
section - The section name as a String.
Returns the deleted section Hash.
Public: Yield each INI file section, parameter, and value in turn to the given block.
block - The block that will be iterated by the each method. The block will
be passed the current section and the parameter / value pair.
Examples
inifile.each do |section, parameter, value| puts "#{parameter} = #{value} [in section - #{section}]" end
Returns this IniFile.
Public: Create a Hash containing only those INI file sections whose names match the given regular expression.
regex - The Regexp used to match section names.
Examples
inifile.match(/^tree_/) #=> Hash of matching sections
Return a Hash containing only those sections that match the given regular expression.
Public: Read the contents of the INI file from the file system and replace and set the state of this IniFile instance. If left unspecified the currently configured filename and encoding will be used when reading from the file system. Otherwise the filename and encoding can be specified in the options hash.
opts - The default options Hash
:filename - The filename as a String :encoding - The encoding as a String (Ruby 1.9)
Returns this IniFile instance if the read was successful; nil is returned if the file could not be read.
Public: Write the contents of this IniFile to the file system. If left unspecified, the currently configured filename and encoding will be used. Otherwise the filename and encoding can be specified in the options hash.
opts - The default options Hash
:filename - The filename as a String :encoding - The encoding as a String (Ruby 1.9)
Returns this IniFile instance.