From: Josef 'Jupp' SCHUGT Date: 2003-11-29T08:56:04+09:00 Subject: Re: Need library for parsing configuration files * T. Onoma; 2003-11-28, 20:42 UTC: > On Friday 28 November 2003 06:48 pm, Gavri Savio Fernandez wrote: > > i'm writing an application which needs to parse a configuration > > file on program startup. is there a prefered format for > > configuration files in ruby? is there a library for such a > > purpose? i suppose i could do the parsing myself, but i was just > > wondering if there is a sophisticated way out there. > > I would consider Yaml. There are at least four ways: - INI - Ruby - XML - Yaml INI files are very simple but also limited. ; .qvwm-theme [Variables] DefaultIcon = "def16.ani" ; titlebar/taskbar-button icon DefaultLargeIcon = "def32.ani" ; task switcher icon DefaultShortcutIcon = "icon32.ani" ; shortcut icon DefaultFont = "-*-*-medium-r-normal-*-14-*-*-*-*-*-*-*" YAML has already been mentioned. In the case of XML at least one libary exists that focusses on configuration files. Personally I prefer Ruby files to use other Ruby files as their configuration files. To give an example of a config file: Conf = { 'stars' => true, 'host' => 'localhost', 'port' => 110, 'user' => 'jupp', 'pass' => '74!GeD,5', 'apop' => false, 'filter' => '/home/jupp/.popclient-filter.rb', 'charset' => 'ISO-8859-15', 'statistics' => true, 'confirmation'=> false, 'deliver' => true, 'filerules' => '/home/jupp/.popclient-filerules.rb', 'logfile' => '/home/jupp/popclient-log', } Actually in use, I only did change value of Conf['pass']. Parsing is then done in this way: def initialize(rcfile) rcfile = Default_rcfile if rcfile.nil? @rcfile = rcfile return unless file_okay?(rcfile, true) begin eval File.new(rcfile).read rescue ScriptError=>e warn("An error occurred while reading #{rcfile}: ", e) else @stars = Conf['stars'] @apop = Conf['apop'] @host = Conf['host'] @user = Conf['user'] @pass = Conf['pass'] @filter = Conf['filter'] @log = Conf['logfile'] @charset = Conf['charset'] @statistics = Conf['statistics'] @confirmation = Conf['confirmation'] @deliver = Conf['deliver'] @filerules = Conf['filerules'] unless Conf['port'].nil? port = Conf['port'] if port < 0 or port > 65535 warn("Invalid port in '#{rcfile}', ignored") else @port = port end end end end file_okay? does access right checking and e.g. complains about world or group readable configuration files (which is not a good idea if the file may contain a valuable password). Josef 'Jupp' Schugt -- begin SPAM-POLICY.txt.vbs if msg.size > 100 kB or msg.sender.is_spammer or msg.text.is_spam discard message end