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...
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...
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
···
On 1/30/07, ChrisKaelin <ck1@stonedragon.ch> 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...
Can you change the structure of your config file?
If you can, I would suggest writing the config file in yaml and using Ruby's built-in YAML stuff like so:
config-file:
Conf:
host: localhost
title: TIITEL
mailhost: mailhost
On Wed, 31 Jan 2007 04:30:06 +0900 "ChrisKaelin" <ck1@stonedragon.ch> 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...
On 1/30/07, James Edward Gray II <james@grayproductions.net> wrote:
On Jan 30, 2007, at 1:30 PM, 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...
Never trust the user! Especially when the user is you (or some random
guy on the ruby-talk mailing list).
As James and Luke have said, YAML is straightforward enough to learn.
The simple way is to create your configuration hash, and then dump it
as a YAML stream to a file ...
On 1/30/07, Luke Ivers <technodolt@gmail.com> wrote:
On Wed, 31 Jan 2007 04:30:06 +0900 > "ChrisKaelin" <ck1@stonedragon.ch> 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'
> }
Can you change the structure of your config file?
If you can, I would suggest writing the config file in yaml and using Ruby's built-in YAML stuff like so:
config-file:
Conf:
host: localhost
title: TIITEL
mailhost: mailhost
Thanks a lot for all your suggestions, what a nice community! I
appreciate that very much, as I'm still learning ruby. I admit YAML
really seems very easy in the first example. My assumption, that YAML
is complicated came from the official documentation, which had no such
easy examples.