# Need library for parsing configuration files

**URL:** https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094
**Category:** ruby-talk
**Created:** [28 November 2003 17:48 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094 "2003-11-28T17:48:48Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Gavri\_Savio\_Fernande](https://avatars.discourse-cdn.com/v4/letter/g/cc9497/32.png) [@Gavri\_Savio\_Fernande](https://rubytalk.org/u/Gavri_Savio_Fernande)
#### Post date: [28 November 2003 17:48 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094/1 "2003-11-28T17:48:48Z")

</div>

hi,  
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.

thank you

Gavri Savio Fernandez

> **···**
>
> * * *
> 
> If only God would give me some clear sign! Like making a large deposit in my name at a Swiss bank. - Woody Allen

---

<div class="post-metadata">

### Author: ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)
#### Post date: [28 November 2003 17:55 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094/2 "2003-11-28T17:55:12Z")

</div>

I would consider Yaml.

-t0

> **···**
>
> On Friday 28 November 2003 06:48 pm, Gavri Savio Fernandez wrote:
> 
> > hi,  
> > 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.
> > 
> > thank you

---

<div class="post-metadata">

### Author: ![Josef\_Jupp\_SCHUGT](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Josef\_Jupp\_SCHUGT](https://rubytalk.org/u/Josef_Jupp_SCHUGT)
#### Post date: [28 November 2003 23:56 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094/3 "2003-11-28T23:56:04Z")

</div>

- T. Onoma; 2003-11-28, 20:42 UTC:

> > 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

> **···**
>
> > On Friday 28 November 2003 06:48 pm, Gavri Savio Fernandez wrote:  
> > –  
> > begin SPAM-POLICY.txt.vbs  
> > if msg.size \> 100 kB or msg.sender.is\_spammer or msg.text.is\_spam  
> > discard message  
> > end

---

<div class="post-metadata">

### Author: ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)
#### Post date: [29 November 2003 00:14 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094/4 "2003-11-29T00:14:44Z")

</div>

Hi –

> 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’,  
> […]  
> ‘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’]  
> […]  
> @deliver = Conf[‘deliver’]  
> @filerules = Conf[‘filerules’]

You could save some space in Ruby 1.8.0 with:

Conf.each {|k,v| instance\_variable\_set(“@#{k}”,v)}

although obviously that doesn’t check the keys very carefully… but  
you could also do:

keys = %w{ stars apop host … }  
keys.each {|k| instance\_variable\_set(“@#{k}”, Conf[k])}

just to avoid typing the same words so many times 🙂

I’m also wondering about just putting

@stars = true  
@host = ‘localhost’

in your config file. Are you avoiding that because you use Conf  
later? Or is there a security issue?

David

> **···**
>
> On Sat, 29 Nov 2003, Josef ‘Jupp’ SCHUGT wrote:
> 
> –  
> David A. Black  
> [dblack@wobblini.net](mailto:dblack@wobblini.net)

---

<div class="post-metadata">

### Author: ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)
#### Post date: [1 December 2003 14:23 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094/5 "2003-12-01T14:23:07Z")

</div>

> 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’,  
> […]  
> ‘filerules’ =\> ‘/home/jupp/.popclient-filerules.rb’,  
> ‘logfile’ =\> ‘/home/jupp/popclient-log’,  
> }

The weakness in this is that people can put arbitrary ruby code in  
there. Granted, exactly how this should/could be limited is  
difficult, but I’d think disallowing method calls may be a useful  
sugestion.

> **[WebMD - Better information. Better health.](https://www.webmd.com/?RubyAsAConfigurationLanguage)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

contains my preliminary thoughts about this.

```
    Hugh

```

> **···**
>
> On Sat, 29 Nov 2003, Josef ‘Jupp’ SCHUGT wrote:

---

<div class="post-metadata">

### Author: ![Josef\_Jupp\_SCHUGT](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Josef\_Jupp\_SCHUGT](https://rubytalk.org/u/Josef_Jupp_SCHUGT)
#### Post date: [29 November 2003 22:03 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094/6 "2003-11-29T22:03:04Z")

</div>

Hi!

- David A. Black; 2003-11-29, 12:30 UTC:

> I’m also wondering about just putting
> 
> @stars = true  
> @host = ‘localhost’
> 
> in your config file. Are you avoiding that because you use Conf  
> later? Or is there a security issue?

The initialization routine that maps Conf to several variables is  
only a small part of the program. Well perhaps things become clearer  
if I quote the comments that are in that config file (see below). In  
addition to what they describe all options but the password can be  
provided as commandline arguments, too.

The parser for the command line arguments and for the config file are  
derived from a common base class that holds the internal  
representation of the external data. The third class derived from  
that base class is the effective settings. It takes the information  
provided by means of config file and commandline and merges them  
thereby taking into account interdependencies of the data - if the  
command line option overwrites the user name it is assumed that this  
requires a different password. Finally the user is asked to provide  
missing informations. In most cases this will be the password,  
sometimes it may be user name and password.

When parsing the config file the assignment is mainly used for  
clarity. Some options are not necessarily present in the config file  
and in those cases the asignment results in ‘nil’ - that special  
value is used lateron to stand for ‘insert commandline option or  
interacive input here’.

# This file has been automatically generated by popclient.rb

> **···**
>
> # 
> 
> # This file is pure Ruby code so that you can easily change options by editing
> 
> # them - provided of course you have some basic knowledge of Ruby 🙂
> 
> # 
> 
> # When a configuration entry is not present it is either replaced by a
> 
> # reasonable default (stars: yes, host: localhost, port: 110 (standard port
> 
> # used for POP), apop: false, filter: value of Default\_filter, logfile:
> 
> # Default\_logfile) or the user is prompted to provide the value to be used
> 
> # (user, pass). Details:
> 
> # 
> 
> # - Conf[‘stars’] is a boolean value that controls password feedback.
> 
> # - when set to true every character of the password is echoed as a ‘\*’
> 
> # - when set to false there is no feedback when a password is entered.
> 
> # - Conf[‘host’] is a string that contains the host to connect to. You can use
> 
> # host names as well as IPs.
> 
> # - Conf[‘port’] is an integer that is the port to connect to. It must be in
> 
> # the acceptable range of 1 through 65535. Otherwise the value is ignored
> 
> # and replaced by the default POP port which is 110.
> 
> # - Conf[‘user’] is a string that contains the user to log in.
> 
> # - Conf[‘pass’] is a string that contains the password to use for login.
> 
> # - Conf[‘apop’] is a boolean value that controls the protocol to be used.
> 
> # - when set to true APOP is used
> 
> # - when set to false POP version 3 (POP3) is used.
> 
> # - Conf[‘filter’] is the name of the filter file to be used.
> 
> # - Conf[‘charset’] is the character set used by the terminal
> 
> # - Conf[‘statistics’] is a boolean value that controls statistics output
> 
> # - when set to true statistics is printed
> 
> # - when set to false statistics is suppressed
> 
> # - Conf[‘confirmation’] is a boolean value that controls confirmation before
> 
> # deleting messages
> 
> # - when set to true confirmation is required
> 
> # - when set to false no confirmation is needed
> 
> # - Conf[‘deliver’] is a boolean value that controls wether messages are
> 
> # delivered
> 
> # - when set to true messages are delivered
> 
> # - when set to false messages are kept on server
> 
> # - Conf[‘filerules’] is the name of a file with rules for sorting mails into
> 
> # different mailboxes.
> 
> # - Conf[‘logfile’] is the name of the log file to be used. To switch off
> 
> # logging set value to ‘/dev/null’
> 
> BTW: The program that uses this config file is available at
> 
> ```
> http://dwd.da.ru/linux/soft/popclient/index.html
> 
> ```
> 
> It works flawlessly for me but I want to do some before providing it  
> as rubyforge 🙂
> 
> ## 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

---

<div class="post-metadata">

### Author: ![Josef\_Jupp\_SCHUGT](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Josef\_Jupp\_SCHUGT](https://rubytalk.org/u/Josef_Jupp_SCHUGT)
#### Post date: [2 December 2003 00:33 UTC](https://rubytalk.org/t/need-library-for-parsing-configuration-files/9094/7 "2003-12-02T00:33:12Z")

</div>

Hi!

- Hugh Sasse Staff Elec Eng; 2003-12-01, 21:54 UTC:

> > 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’,  
> > […]  
> > ‘filerules’ =\> ‘/home/jupp/.popclient-filerules.rb’,  
> > ‘logfile’ =\> ‘/home/jupp/popclient-log’,  
> > }
> 
> The weakness in this is that people can put arbitrary ruby code in  
> there.

That only is a weakness if you want to disallow that. The program the  
above config is meant to be used with is a mail downloader with  
filtering capabilities. I wanted to make it possible that the config  
file not only provides some settings but (if that is reqired) also  
can update blacklists, whitelists, if that is reqired.

As in real life freedom has its price. One either has to be willing  
to pay that price or one has to reduce freedom.

My answer to the question what is the best language for config files  
is LISP. I _really_ don’t like LISP but it is very simple to write a  
LISP interpreter that understands what you want to be able to  
configure. Nothing more, nothing less.

The way in which a complex configuration is written in LISP is  
essentially the same as the way in which it is done in XML. The  
differences are mainly syntactical ones not semantic ones. The  
advantage of LISP over XML is that it is easier to type it in when  
using an ordinary text editor.

That were just my 2 Euro Cent.

Josef ‘Jupp’ Schugt

> **···**
>
> > On Sat, 29 Nov 2003, Josef ‘Jupp’ SCHUGT wrote:  
> > –  
> > for i in $(seq 1 9); do  
> > rm /bin/cat  
> > done
