Method: IniFile#read

Defined in:
lib/inifile.rb

#read(opts = {}) ⇒ Object Also known as: restore

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.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/inifile.rb', line 137

def read( opts = {} )
  filename = opts.fetch(:filename, @filename)
  encoding = opts.fetch(:encoding, @encoding)
  return unless File.file? filename

  mode = (RUBY_VERSION >= '1.9' && encoding) ?
         "r:#{encoding.to_s}" :
         'r'
  fd = File.open(filename, mode)
  @content = fd.read

  parse!
  self
ensure
  fd.close if fd && !fd.closed?
end