lib/inireader/base.rb in inireader-0.0.1 vs lib/inireader/base.rb in inireader-0.1.1

- old
+ new

@@ -1,39 +1,37 @@ -module IniReader - class Base - def initialize( string = '') - @section_hash = {} - parse(StringIO.new(string)) - end +class IniReader + def initialize( string = '') + @section_hash = {} + parse(StringIO.new(string)) + end - def self.parse(filename) - reader = IniReader::Base.new - reader.send(:parse, File.open(filename, "r")) - return reader - end + def self.parse(filename) + reader = IniReader.new + reader.send(:parse, File.open(filename, "r")) + return reader + end - #returns the hash for a section of the inifile - def []( section_name ) - return @section_hash[section_name] - end + #returns the hash for a section of the inifile + def []( section_name ) + return @section_hash[section_name] + end - private - def parse(io) - current_section = nil - io.each do |line| - if line[0] == ';' - next - elsif ( matchdata = line.match(/^\[(.+)\]$/) ) != nil - current_section = @section_hash[matchdata[1].to_sym] || {} - @section_hash[matchdata[1].to_sym] = current_section - elsif ( matchdata = line.match(/^([^=]*)=(.*)$/) ) - current_section[matchdata[1].to_sym] = matchdata[2].to_s - elsif line.strip == "" - next - else - raise Exception("Could not parse line: #{line}") - end + private + def parse(io) + current_section = nil + io.each do |line| + line = line.strip + if line[0] == ';' + next + elsif ( matchdata = line.match(/^\[(.+)\]$/) ) != nil + current_section = @section_hash[matchdata[1].to_sym] || {} + @section_hash[matchdata[1].to_sym] = current_section + elsif ( matchdata = line.match(/^([^=]*)=(.*)$/) ) + current_section[matchdata[1].to_sym] = matchdata[2].to_s + elsif line.strip == "" + next + else + raise Exception("Could not parse line: #{line}") end end - end end \ No newline at end of file