From: "David A. Black" Date: 2003-11-29T09:14:44+09:00 Subject: Re: Need library for parsing configuration files Hi -- On Sat, 29 Nov 2003, Josef 'Jupp' SCHUGT wrote: > Personally I prefer Ruby files to use other Ruby files as their > configuration files. To give an example of a config file: > > Conf = { > 'stars' => true, > 'host' => 'localhost', [...] > 'logfile' => '/home/jupp/popclient-log', > } > > Actually in use, I only did change value of Conf['pass']. Parsing is > then done in this way: > > def initialize(rcfile) > rcfile = Default_rcfile if rcfile.nil? > @rcfile = rcfile > return unless file_okay?(rcfile, true) > begin > eval File.new(rcfile).read > rescue ScriptError=>e > warn("An error occurred while reading #{rcfile}: ", e) > else > @stars = Conf['stars'] > @apop = Conf['apop'] [...] > @deliver = Conf['deliver'] > @filerules = Conf['filerules'] You could save some space in Ruby 1.8.0 with: Conf.each {|k,v| instance_variable_set("@#{k}",v)} although obviously that doesn't check the keys very carefully.... but you could also do: keys = %w{ stars apop host .... } keys.each {|k| instance_variable_set("@#{k}", Conf[k])} just to avoid typing the same words so many times :-) I'm also wondering about just putting @stars = true @host = 'localhost' in your config file. Are you avoiding that because you use Conf later? Or is there a security issue? David -- David A. Black dblack@wobblini.net