Method: IniFile#initialize

Defined in:
lib/inifile.rb

#initialize(content = nil, opts = {}) ⇒ IniFile

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


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/inifile.rb', line 76

def initialize( content = nil, opts = {} )
  opts, content = content, nil if Hash === content

  @content = content

  @comment  = opts.fetch(:comment, ';#')
  @param    = opts.fetch(:parameter, '=')
  @encoding = opts.fetch(:encoding, nil)
  @escape   = opts.fetch(:escape, true)
  @default  = opts.fetch(:default, 'global')
  @filename = opts.fetch(:filename, nil)

  @ini = Hash.new {|h,k| h[k] = Hash.new}

  if    @content  then parse!
  elsif @filename then read
  end
end