From: Sean Russell Date: 2002-01-19T01:38:48+09:00 Subject: Submission: Ruby shard for config files Here's a code snippit I find myself using quite a bit. I thought others might find it useful. module Config def load file lines = IO.readlines(file) lines.collect! do |line| line.scan(/(((["']).*\3)|\S+)/). collect { |ar| ar[0] } unless /^\s*(#.*)?$/ end lines.each do |args| self.send( *args ) end end end Example usage: require "config" class MyConf include Config attr_reader :files, :commands def initialize file @files = [] @commands = {} load file end def watch(file) @files << file end def command file, command @commands[file] = command end end conf = MyConf.new "/etc/myapp.conf" Here's a sample config file: watch /tmp/somefile.txt watch /etc/someotherfile.txt command something "cat something" Please note that there is a measure of insecurity to using this code, if you don't protect the config file. If you want a (more) secure version, do the following: replace the following lines lines.each do |args| self.send( *args ) end with lines.each do |args| self.send( *args ) if protected_methods.include?(args[0]) end and then protect all of your configuration methods. There are probably other ways of doing this, but this ensures that only the methods you specify will be called. -- --- SER