# Config files

**URL:** <https://rubytalk.org/t/config-files/41090>\
**Category:** ruby-talk\
**Created:** [1 October 2007 23:21 UTC](https://rubytalk.org/t/config-files/41090 "2007-10-01T23:21:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [1 October 2007 23:21 UTC](https://rubytalk.org/t/config-files/41090/1 "2007-10-01T23:21:11Z")

</div>

I am writing an application that will read and grab values from a  
configuration file. I am wondering what peoples opinions are on what to  
use. YAML? XML? Does anyone have any preference? Are any better then the  
others or is it just down to user preference?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Konrad\_Meyer](https://avatars.discourse-cdn.com/v4/letter/k/df705f/32.png) [@Konrad\_Meyer](https://rubytalk.org/u/Konrad_Meyer)\
**Post date:** [2 October 2007 00:20 UTC](https://rubytalk.org/t/config-files/41090/2 "2007-10-02T00:20:27Z")

</div>

Quoth Lee Jarvis:

> I am writing an application that will read and grab values from a  
> configuration file. I am wondering what peoples opinions are on what to  
> use. YAML? XML? Does anyone have any preference? Are any better then the  
> others or is it just down to user preference?
> 
> Thanks

Personally, I think XML is a load of crap. If your configuration is simple  
enough (and it likely is) I'd use YAML. If you need anything more  
complicated, you can always use your own scheme, Marshal, or a sqlite db or  
something.

Regards,

> **···**
>
> --  
> Konrad Meyer \<konrad@tylerc.org\> [http://konrad.sobertillnoon.com/](http://konrad.sobertillnoon.com/)

---

<div class="post-metadata">

**Author:** ![BJ\_Dierkes](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@BJ\_Dierkes](https://rubytalk.org/u/BJ_Dierkes)\
**Post date:** [2 October 2007 03:26 UTC](https://rubytalk.org/t/config-files/41090/3 "2007-10-02T03:26:53Z")

</div>

I prefer using standard \*nix style configuration files in the form of 'param = value'. I wrote a class to read this type of configuration file called ParseConfig.

[http://rubyforge.org/projects/parseconfig/](http://rubyforge.org/projects/parseconfig/)

It is available via rubygems/rubyforge:

gem install parseconfig

Usage is really simple. Say you have a config file that looks like:

> **···**
>
> ---  
> user = 'username'  
> pass = 'password'  
> log\_file = '/var/log/mylog'  
> ---
> 
> A quick example to use this config would be:  
> &nbsp;&nbsp;---  
> require('rubygems')  
> require('parseconfig')
> 
> config = ParseConfig.new('/path/to/config/file')  
> user = config.get\_value('user')  
> pass = config.get\_value('pass')  
> log\_file = config.get\_value('log\_file')  
> ---
> 
> If you all have any suggestions, etc... you can submit them via the RubyForge Tracker.
> 
> On Oct 1, 2007, at 6:21 PM, Lee Jarvis wrote:
> 
> > I am writing an application that will read and grab values from a  
> > configuration file. I am wondering what peoples opinions are on what to  
> > use. YAML? XML? Does anyone have any preference? Are any better then the  
> > others or is it just down to user preference?
> > 
> > Thanks  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Paul\_Van\_Delst](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@Paul\_Van\_Delst](https://rubytalk.org/u/Paul_Van_Delst)\
**Post date:** [2 October 2007 22:40 UTC](https://rubytalk.org/t/config-files/41090/4 "2007-10-02T22:40:15Z")

</div>

Lee Jarvis wrote:

> I am writing an application that will read and grab values from a  
> configuration file. I am wondering what peoples opinions are on what to  
> use. YAML? XML? Does anyone have any preference? Are any better then the  
> others or is it just down to user preference?

I tried to roll my own once - and now I use yaml. :o) Nice and simple, easy to use for both simple and complicated[\*] configuration files, and you can go on with more interesting stuff.

Besides, whichever you choose, you can always change it if your application outgrows it, right?

cheers,

paulv

[\*] Well, actually, in this case it wasn't a configuration file per se, but a specification for a derived type definition in Fortran95 along with associated netcdf attributes for I/O. Works a treat, though.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [2 October 2007 23:00 UTC](https://rubytalk.org/t/config-files/41090/5 "2007-10-02T23:00:33Z")

</div>

YAML tends to work out well, when it's very simple data fields being collected. That said, YAML's syntax rules are quite complex and asking a user to edit any non-trivial data in the format is a bad idea, in my opinion.

If you need markup at all, use XML. It's a markup language, after all. YAML is not.

Also, while more verbose, XML has a much easier syntax that far more people are familiar with. I'm not saying that means you have to use it, but it's something to stay aware of.

Finally, I've used this trick a couple of times now and I just love it:

#!/usr/bin/env ruby -wKU

require "ostruct"

module Config  
&nbsp;&nbsp;&nbsp;module\_function

&nbsp;&nbsp;&nbsp;def load\_config\_file(path)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval \<\<-END\_CONFIG  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config = OpenStruct.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{File.read(path)}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END\_CONFIG  
&nbsp;&nbsp;&nbsp;end  
end

\_\_END\_\_

That let's me do my configurations in pure Ruby, which may or may not be appropriate for your needs.

I guess the most important thing is to consider your audience.

James Edward Gray II

> **···**
>
> On Oct 1, 2007, at 6:21 PM, Lee Jarvis wrote:
> 
> > I am writing an application that will read and grab values from a  
> > configuration file. I am wondering what peoples opinions are on what to  
> > use. YAML? XML? Does anyone have any preference? Are any better then the  
> > others or is it just down to user preference?

---

<div class="post-metadata">

**Author:** ![Dejan\_Dimic](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Dejan\_Dimic](https://rubytalk.org/u/Dejan_Dimic)\
**Post date:** [2 October 2007 06:50 UTC](https://rubytalk.org/t/config-files/41090/6 "2007-10-02T06:50:02Z")

</div>

As told so menu times before: There is no golden hammer.  
The optimal solution depends on your application needs and estimation  
about future use of it.  
The yaml is more human readable. XML is universal and can be used by  
other applications but has some overhead. Classical config files are  
something programmers used to and it's portable and more or less human  
readable. There is also a data base configuration but that is out of  
the scope because you should have configuration for database that  
holds configurations for application.

Personally, the yaml version is a way to start. If some need emerge  
that can't be natively expressed by yaml than you should move to other  
solution. But most probably you will stay on yaml.

> **···**
>
> On Oct 2, 5:26 am, BJ Dierkes \<wdier...@5dollarwhitebox.org\> wrote:
> 
> > I prefer using standard \*nix style configuration files in the form of  
> > 'param = value'. I wrote a class to read this type of configuration  
> > file called ParseConfig.
> > 
> > [http://rubyforge.org/projects/parseconfig/](http://rubyforge.org/projects/parseconfig/)
> > 
> > It is available via rubygems/rubyforge:
> > 
> > gem install parseconfig
> > 
> > Usage is really simple. Say you have a config file that looks like:
> > 
> > ---  
> > user = 'username'  
> > pass = 'password'  
> > log\_file = '/var/log/mylog'  
> > ---
> > 
> > A quick example to use this config would be:  
> > &nbsp;&nbsp;---  
> > require('rubygems')  
> > require('parseconfig')
> > 
> > config = ParseConfig.new('/path/to/config/file')  
> > user = config.get\_value('user')  
> > pass = config.get\_value('pass')  
> > log\_file = config.get\_value('log\_file')  
> > ---
> > 
> > If you all have any suggestions, etc... you can submit them via the  
> > RubyForge Tracker.
> > 
> > On Oct 1, 2007, at 6:21 PM, Lee Jarvis wrote:
> > 
> > \> I am writing an application that will read and grab values from a  
> > \> configuration file. I am wondering what peoples opinions are on  
> > \> what to  
> > \> use. YAML? XML? Does anyone have any preference? Are any better  
> > \> then the  
> > \> others or is it just down to user preference?
> > 
> > \> Thanks  
> > \> --  
> > \> Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Phil4](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@Phil4](https://rubytalk.org/u/Phil4)\
**Post date:** [2 October 2007 06:55 UTC](https://rubytalk.org/t/config-files/41090/7 "2007-10-02T06:55:03Z")

</div>

I like using Ruby directly for doing stuff like this (if the "configuration file" is under my control).

Example:  
module MyConfig  
&nbsp;&nbsp;&nbsp;user = 'username'  
&nbsp;&nbsp;&nbsp;pass = 'password'  
&nbsp;&nbsp;&nbsp;log\_file = '/var/log/mylog'  
end  
=\> Store in file 'MyConfig.rb'

test.rb:  
require 'MyConfig'

p MyConfig::User  
p MyConfig::Pass  
p MyConfig::Log\_file

BR Phil Meier

BJ Dierkes schrieb:

> **···**
>
> > I prefer using standard \*nix style configuration files in the form of 'param = value'. I wrote a class to read this type of configuration file called ParseConfig.
> > 
> > [http://rubyforge.org/projects/parseconfig/](http://rubyforge.org/projects/parseconfig/)
> > 
> > It is available via rubygems/rubyforge:
> > 
> > gem install parseconfig
> > 
> > Usage is really simple. Say you have a config file that looks like:
> > 
> > ---  
> > user = 'username'  
> > pass = 'password'  
> > log\_file = '/var/log/mylog'  
> > ---
> > 
> > A quick example to use this config would be:  
> > ---  
> > require('rubygems')  
> > require('parseconfig')
> > 
> > config = ParseConfig.new('/path/to/config/file')  
> > user = config.get\_value('user')  
> > pass = config.get\_value('pass')  
> > log\_file = config.get\_value('log\_file')  
> > ---
> > 
> > If you all have any suggestions, etc... you can submit them via the RubyForge Tracker.
> > 
> > On Oct 1, 2007, at 6:21 PM, Lee Jarvis wrote:
> > 
> > > I am writing an application that will read and grab values from a  
> > > configuration file. I am wondering what peoples opinions are on what to  
> > > use. YAML? XML? Does anyone have any preference? Are any better then the  
> > > others or is it just down to user preference?
> > > 
> > > Thanks  
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Terry\_Poulin](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@Terry\_Poulin](https://rubytalk.org/u/Terry_Poulin)\
**Post date:** [3 October 2007 21:23 UTC](https://rubytalk.org/t/config-files/41090/8 "2007-10-03T21:23:36Z")

</div>

BJ Dierkes wrote:

> I prefer using standard \*nix style configuration files in the form of  
> 'param = value'. I wrote a class to read this type of configuration  
> file called ParseConfig.
> 
> [http://rubyforge.org/projects/parseconfig/](http://rubyforge.org/projects/parseconfig/)

Ooo, thank you.. I've been meaning to look for some thing like that 🙂

I have actually never needed to deal with config files from this perspective.  
Personally from the point of view of Joe Blow. I prefer a syntax like most  
unix style config files.

# Comment  
parm = value

Along with the ini file, it makes for a very easy to edit file. It's just a  
matter of figuring out what does what for the end user. (IMHO)

TerryP.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> Email and shopping with the feelgood factor!  
> 55% of income to good causes. [http://www.ippimail.com](http://www.ippimail.com)

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [3 October 2007 21:40 UTC](https://rubytalk.org/t/config-files/41090/9 "2007-10-03T21:40:14Z")

</div>

Config files should be plain Ruby (written of the top of my head). Why parse  
for yourself when Ruby has a perfectly fine one?

config.ini

param1 = value1  
param2 = value2  
...

parser.rb

def method\_missing(method\_name, \*args)  
&nbsp;&nbsp;if method\_name.match(/=/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@config[method\_name] = args[0]  
&nbsp;&nbsp;end  
end

require "config.ini"

@config["param1"] # =\> value1

This of course, or YAML, because that's just so bloody easy.

Jason

> **···**
>
> On 10/3/07, Terry Poulin \<bigboss64@ippimail.com\> wrote:
> 
> > BJ Dierkes wrote:  
> > \> I prefer using standard \*nix style configuration files in the form of  
> > \> 'param = value'. I wrote a class to read this type of configuration  
> > \> file called ParseConfig.  
> > \>  
> > \> [http://rubyforge.org/projects/parseconfig/](http://rubyforge.org/projects/parseconfig/)  
> > \>  
> > \>
> > 
> > Ooo, thank you.. I've been meaning to look for some thing like that 🙂
> > 
> > I have actually never needed to deal with config files from this  
> > perspective.  
> > Personally from the point of view of Joe Blow. I prefer a syntax like most  
> > unix style config files.
> > 
> > # Comment  
> > parm = value
> > 
> > Along with the ini file, it makes for a very easy to edit file. It's just  
> > a  
> > matter of figuring out what does what for the end user. (IMHO)
> > 
> > TerryP.
> > 
> > --
> > 
> > Email and shopping with the feelgood factor!  
> > 55% of income to good causes. [http://www.ippimail.com](http://www.ippimail.com)
