I want to use configuration files in order to save values, directories,
and files. I did some googeling to find out that the ruby community
often suggests ruby-files (hashes) to save configurations. These
rb-files are read in by interpreting. (btw, anyone suggests a cool
part of code for this with errorhandling and so on?)
I want to use configuration files in order to save values,
directories, and files. I did some googeling to find out that the
ruby community often suggests ruby-files (hashes) to save
configurations. These rb-files are read in by interpreting. (btw,
anyone suggests a cool part of code for this with errorhandling and
so on?)
If I understand what you’re asking for, you may want to have a look at
YAML. Your config file can look like this:
adapter: mysql
database: demodb
username: karl
password: pa$$w0rd
socket: /var/lib/mysql/mysql.sock
Turning that file into a ruby hash is a one-liner…
I’m not sure about that. YAML has the concept of “aliases” and
“anchors” which are close to what you’re asking for, but I’m not sure
it would be possible to concatenate an anchor with a plain string.
Perhaps someone more YAML-savvy will chime in.
I wrote something a bit more general. It assumes that the yaml file
serializes a Ruby hash. You’d probably want this to be a singleton
class, but my uses don’t require that. Perhaps the OP would find it
useful:
# This creates a global variable for each entry in the yaml
configuration file.
# I recommend that this interface be deprecated in favor of using
accessor syntax.
# E.g. creating a Parameters object config = Parameters.new and
calling accessors
# on config.
@config.keys.each do |parameter|
eval('$' + "#{parameter} = @config[parameter]")
end
# This creates an accessor for each of the configuration entries in
the yaml
# configuration file.
@config.keys.each do |parameter|
eval("@#{parameter} = @config[parameter]")
self.class.class_eval {attr_reader parameter}
end
end
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.