From: Tim Pease Date: 2007-01-31T04:39:57+09:00 Subject: Re: Howto parse or eval a config-file? On 1/30/07, ChrisKaelin wrote: > I'm currently looking for a simple way to parse a configuration file > in a ruby-script. Is eval a good way to do that? YAML seems a bit an > overkill to learn for such a small task... > > Thanks in advance for any suggestions. > > So far I'm doing this: > > the config-file: > > Conf = { > 'host' => 'localhost', > 'title' => 'TIITEL', > 'mailhost' => 'mailhost' > } > > the eval-code: > > begin > eval File.new(configFile).read > rescue ScriptError=>e > warn("An error occurred while reading #{$configFile}: ", e) > else > $host = Conf['host'] > $title = Conf['title'] > $mailhost = Conf['mailhost'] > end > Try using load instead ... begin load $configFile resuce Exception => e warn("An error occurred while reading #{$configFile}: ", e) end And just use the configuration hash directly instead of creating lots of global variables -- i.e. change Conf = { in your config file to $conf = { That way all your classes can just grab what they need straight from $conf. Beware of naming collisions in the global variable namespace. Someone else might think $conf is a great place to store their configuration items, too. Blessings, TwP