Hi,
a little follow-up on loading config data as Ruby source files. I want my
project to load some full system defaults from a file, and then some
user-specified overrides from another file, and finally some special
overrides that are set on the commandline or from the calling environment.
I would like the variables with their values to be accessible as constants,
because I think that is faster than hash lookups, and it looks elegant,
because they will get referenced often from other script snippets that get
loaded after the config is read.
I figure that I can use modules to carry constants. Eg.
module Conf
include load (“userdefaults.conf”, true)
include load (“systemdefaults.conf”, true)
end
(This is done once, when starting the program.)
Now the Conf module contains all constants from systemdefaults,
but overridden by userdefaults (they are not overwritten by the system
defaults, because constants are not changed by include when they are set.
nou create a new class to hold new specified settings
(This can occur often during a session)
confc = Class.new ( OurBaseClass ) # OurBaseClass contains methods etc.
confc.const_set “CFLAGS”, “our value” # specific setting
confc.const_set “VAR2”, 1234
confc.class_eval “include Conf”
object = confc.new
our object now has all constants defined as needed, and all the methods from
OurBaseClass.
The constants will be referenced to like in my previous thread, by little
snippets of Ruby code loaded on demand.
My question: is this the most elegant (and fastest) way to do this? Or should
I use hashes where usersettings can simply overwrite system settings. The
code snippets then can’t just to:
ALSA && depend "audio/alsa-driver"
but must look like:
uses[“ALSA”] && depend "audio/alsa-driver"
or, using a method,
use “ALSA” && depend “audio/alsa-driver”
The 1st one really looks best to me.
Esp. wat is bothering me a little is that I need to create a new class for
every new object that I want to spawn that needs possibly different settings
for a few constants (user- and sys-defaults remain the same)
So what could be the most efficient way to set up cascading config options
like this?
thanks for all your help and thoughts!
Wilbert
Wilbert Berendsen (http://www.xs4all.nl/~wbsoft/)
To understand recursion, one must first understand recursion.