|
Hi You might find this useful: class MissingConfigurationFile < ArgumentError ; end class MyConfig MyConfig
= Struct.new("MyConfig", :property,
:value) def
initialize(config_file) begin
@config = File.readlines(config_file) rescue
raise MissingConfigurationFile,
"Can't find config file" end end def
parse_config_file rem_array = [] prop_array = [] @config.each do |line|
if line.match(/^#/)
rem_array << line
else
unless line.match(/^\n$/)
then prop_array << line.chomp
end
end end return prop_array - rem_array end def
properties config_hash = {} parse_config_file.each do |data|
split = data.split("=")
property = split[0].gsub(/\s*/,
"")
value = split[1].gsub(/$\s*/,
"").gsub(/^\s*/, "")
config_hash.store(property,MyConfig.new(property, value)) end return config_hash end end myConfig = MyConfig.new(“config.txt”).properties config_option_name = myConfig[‘OPTION_NAME].property config_option_value = myConfig[‘OPTION_NAME’].value config.txt # Lines with a # are ignored OPTION_NAME = put some value here OPTION_2 = more options Hope it’s useful Thanks Kingsley -----Original Message----- This
email is to be read subject to the disclaimer below. |
_______________________________________________ Wtr-general mailing list [email protected] http://rubyforge.org/mailman/listinfo/wtr-general
