Q:

parse a ini like file with ruby

I have a file in the following format:
[X:10]
[Y:20]
# many of them, they are test parameters
C = 1
A = 1234
B = 12345
....
# many of them, they are test cases

# new test parameters
[X:20]
[Y:40]
# new test cases
C = 1
A = 1234
B = 12345
...

It is a test framework. The header (section on [] set the parameters and then the following fields are test cases) 
I parse them today in C. So basically i do the following (as usual in C):
while(fgets(....)
  if(!strcmp(keyword,"[X"))
     x = atoi(value);

however I would like to port it to ruby, in a ruby way: organizing it as classes.
I would like to know if there is any framework (ini parses, doesnt help) to do it.. any idea, framework (treetop, citrus are kind of overkill) or snippet that could help me?
I though something like:
class TestFile
  attr_accessor :sections
  def parse
  end
end

# the test parameters value
class Section
  attr_accessor :entries, foo,bar.. # all accessible fields
end

# the test cases
class Entry
  attr_accessor #all accessible fields
end

then I could use it like:
t = TestFile.new "mytests.txt"
t.parse
t.sections.first

=>    
<Section:0x000000014b6b70 @parameters={"X"=>"128", "Y"=>"96", "Z"=>"0"}, @cases=[{"A"=>"14", "B"=>"1", "C"=>"2598", "D"=>"93418"},{"A"=>"12", "B"=>"3", "C"=>"2198", "D"=>"93438"}] 

any help or direction?

A:

Here's what I came up with. First, the usage:
t = Testfile.new('ini.txt')
t.parse

t.sections.count
#=>2

t.sections.first
#=> #<Section:0x00000002d74b30 @parameters={"X"=>"10", "Y"=>"20"}, @cases={"C"=>"1", "A"=>"1234", "B"=>"12345"}>

As you can see, I made the Section contain both the parameters and the cases--just a judgement call, it could be done other ways. The implementation:
class Testfile
  attr_accessor :filename, :sections

  def initialize(filename)
    @sections = []
    @filename = filename
  end

  def parse
    @in_section = false
    File.open(filename).each_line do |line|
      next if line =~ /^#?\s*$/ #skip comments and blank lines
      if line.start_with? "["
        if not @in_section
          @section = Section.new
          @sections << @section
        end
        @in_section = true
        key, value = line.match(/\[(.*?):(.*?)\]/).captures rescue nil
        @section.parameters.store(key, value) unless key.nil?
      else
        @in_section = false
        key, value = line.match(/(\w+) ?= ?(\d+)/).captures rescue nil
        @section.cases.store(key, value) unless key.nil?
      end
    end
    @sections << @section
  end
end

class Section
  attr_accessor :parameters, :cases

  def initialize
    @parameters = {}
    @cases = {}
  end
end

Most of this code is the parsing. It looks for a line beginning with [ and creates a new Section object (unless it is already parsing a section). Any other non-comment line is parsed as a test case. 

