Here's a script I wrote for a few projects I got going on. It simply parses a configuration file, but in a different way than any that I have seen. Instead of referencing them by hash keys, they stored as methods and structs.
So instead of, for instance, getting the font name this way:
my_font = @config['FONT']['name']
You can use this:
my_font = @config.font.name
Here it is. Sorry for using quote tags. It wouldn't display properly with code tags because of the encoding, and I'm not sure which one I need for posting here.
Quote# Ini.rb - parses and reads .ini files
#
# Author: Eric "ForeverZer0" Freed
#
# Overview:
#
# Class with methods to parse and read from .ini files.
# Different method to store data within methods of the same name, as
# opposed to referencing with hash keys.
#
# ex. @config = Ini.new('C:/Users/User/Documents/config.ini')
#
# Instead of other Ini docs that you would have to get values from
# with this method:
#
# size = @config['FONT']['size']
#
# You can simply use this:
#
# size = @config.font.size
#
# This keeps things a little easier to use and over-view.
#
class Ini
attr_accessor :path
def initialize(path)
@path = path
parse_file
end
def parse_file
data, header = {}, nil
# Create array of lines and iterate over them
IO.readlines(@path).each {|line|
# Remove all comment lines
line = line.strip.split("#")[0]
# Find lines that are words contained in brackets
if line[0] == '[' && line[line.length - 1, line.length] == "]"
# Remove brackets and unnecessary spaces, then store as header
header = line[1, line.length - 2 ].strip.downcase
data[header] = {}
else
# Split line into two element array, parameters and values.
parameter, value = line.split('=', 2)
# Remove spaces of the parameters and values.
[parameter, value].collect! {|string| string.split if string != nil }
# Add data to the last defined header if it is defined.
if header != nil
data[header][parameter] = value
end
end
}
structure_data(data)
end
def structure_data(data)
# Iterate through the defined headers.
data.each_pair {|header, parameters|
# Create a struct, using the paramter names as instance variables.
struct = Struct.new(*(parameters.keys.collect {|para| para.downcase.to_sym } ))
# Set method with header name, using created struct, which will be
# instantized with the respective values.
self.define_method(header) { struct.new(*parameters.values) }
}
end
end