# Using ruby for config files

**URL:** <https://rubytalk.org/t/using-ruby-for-config-files/43640>\
**Category:** ruby-talk\
**Created:** [9 January 2008 08:10 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640 "2008-01-09T08:10:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Furtive\_Clown](https://avatars.discourse-cdn.com/v4/letter/f/3ec8ea/32.png) [@Furtive\_Clown](https://rubytalk.org/u/Furtive_Clown)\
**Post date:** [9 January 2008 08:10 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640/1 "2008-01-09T08:10:01Z")

</div>

(In reference to [http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/1c1fa0bfca68084f/](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/1c1fa0bfca68084f/)  
)

I eventually settled on the below for my config file purposes. I  
wanted configuration to be convenient and flexible. Using XML or YAML  
robs me of the occasional #map, for example. Using a hash for  
configuration is too error-prone; I should assume those doing the  
configuring have no knowledge of ruby. That is,

foo = "bar"  
size = 44

is a better config file than

{  
&nbsp;&nbsp;&nbsp;:foo =\> "bar",  
&nbsp;&nbsp;&nbsp;:size =\> 44,  
}

So here is what I use:

module ConfigReader\_m  
&nbsp;&nbsp;&nbsp;module Util  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.file\_contents(filename)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.open(filename) { |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.read  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.no\_verbose  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;previous\_verbose = $VERBOSE  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$VERBOSE = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$VERBOSE = previous\_verbose  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_each\_pair(config\_code)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;previous\_locals = local\_variables  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_locals = eval(config\_code + "\n" + "local\_variables")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(config\_locals - previous\_locals).each { |name|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(name, eval(name))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_to\_hash(config\_code)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash = Hash.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_each\_pair(config\_code) { |name, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash[name] = value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_to\_open\_struct(config\_code)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'ostruct'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenStruct.new(config\_to\_hash(config\_code))  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_to\_instance\_variables(config\_code)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_each\_pair(config\_code) { |name, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ivar = "@" + name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;existing\_value = Util.no\_verbose {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_get(ivar)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if existing\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "instance variable already set: #{name}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set(ivar, value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_file\_each\_pair(config\_file)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_each\_pair(Util.file\_contents(config\_file))  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_file\_to\_hash(config\_file)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_to\_hash(Util.file\_contents(config\_file))  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_file\_to\_open\_struct(config\_file)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_to\_open\_struct(Util.file\_contents(config\_file))  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def config\_file\_to\_instance\_variables(config\_file)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_to\_instance\_variables(Util.file\_contents(config\_file))  
&nbsp;&nbsp;&nbsp;end  
end

class ConfigReader  
&nbsp;&nbsp;&nbsp;include ConfigReader\_m  
end

class Foo  
&nbsp;&nbsp;&nbsp;include ConfigReader\_m

&nbsp;&nbsp;&nbsp;def initialize(config)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_to\_instance\_variables(config)  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_config  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "@version: "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "@format\_spec: "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @format\_spec  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "@source\_files: "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @source\_files  
&nbsp;&nbsp;&nbsp;end  
end

config = %q{  
&nbsp;&nbsp;&nbsp;version = 3  
&nbsp;&nbsp;&nbsp;format\_spec = "format.xml"  
&nbsp;&nbsp;&nbsp;source\_files = %w(a b c).map { |base|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base + ".cxx"  
&nbsp;&nbsp;&nbsp;}  
}

def sep(header)  
&nbsp;&nbsp;&nbsp;puts "-"\*10 + header  
end

sep("config\_each\_pair")  
ConfigReader.new.config\_each\_pair(config) { |key, value|  
&nbsp;&nbsp;&nbsp;print "#{key}: "  
&nbsp;&nbsp;&nbsp;p value  
}

sep("config\_to\_hash")  
p ConfigReader.new.config\_to\_hash(config)

sep("config\_to\_open\_struct")  
p ConfigReader.new.config\_to\_open\_struct(config)

sep("config\_to\_instance\_variables")  
Foo.new(config).test\_config

output:

----------config\_each\_pair  
version: 3  
format\_spec: "format.xml"  
source\_files: ["a.cxx", "b.cxx", "c.cxx"]  
----------config\_to\_hash  
{"source\_files"=\>["a.cxx", "b.cxx", "c.cxx"], "version"=\>3,  
"format\_spec"=\>"format.xml"}  
----------config\_to\_open\_struct  
#\<OpenStruct version=3, format\_spec="format.xml",  
source\_files=["a.cxx", "b.cxx", "c.cxx"]\>  
----------config\_to\_instance\_variables  
@version: 3  
@format\_spec: "format.xml"  
@source\_files: ["a.cxx", "b.cxx", "c.cxx"]

Since OpenStruct is often the nicest choice, and since ostruct is  
underused by newcomers (both my opinion only), I provided it for  
convenience.

If you wish to provide a restricted list of local variables which will  
become configuration parameters (only), you can do this:

config = %q{  
&nbsp;&nbsp;&nbsp;version = 3  
&nbsp;&nbsp;&nbsp;format\_spec = "format.xml"  
&nbsp;&nbsp;&nbsp;source\_files = %w(a b c).map { |base|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base + ".cxx"  
&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;local\_variables = ["version", "format\_spec"]  
}

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [9 January 2008 09:00 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640/2 "2008-01-09T09:00:06Z")

</div>

Hmm the config style looks nice, but the code looks a bit complex?

I use yaml right now even though it has a few disadvantages (for me in  
this regard) because of indent (where a user may get parse errors).  
And from this yaml info I generate or build system info, on top of it.

I think what would be cool, would be a minimal "config" interpreter with  
only  
a very few (but still quite) readable lines of ruby code. (Anyway,  
that's just my opinion)

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

---

<div class="post-metadata">

**Author:** ![Karl\_von\_Laudermann](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karl\_von\_Laudermann](https://rubytalk.org/u/Karl_von_Laudermann)\
**Post date:** [9 January 2008 14:45 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640/3 "2008-01-09T14:45:05Z")

</div>

If you just want to have configuration files that are little more than  
key/value pairs, why not use the Windows INI file format? There are  
already ruby gems that provide read/write access to INI files:

$ gem list ini -r -d

\*\*\* REMOTE GEMS \*\*\*

ini (0.1.1)  
&nbsp;&nbsp;&nbsp;&nbsp;INI file reader and writer

inifile (0.1.0)  
&nbsp;&nbsp;&nbsp;&nbsp;INI file reader and writer

> **···**
>
> On Jan 9, 3:09 am, furtive.cl...@gmail.com wrote:
> 
> > I eventually settled on the below for my config file purposes. I  
> > wanted configuration to be convenient and flexible. Using XML or YAML  
> > robs me of the occasional #map, for example. Using a hash for  
> > configuration is too error-prone; I should assume those doing the  
> > configuring have no knowledge of ruby. That is,
> > 
> > foo = "bar"  
> > size = 44

---

<div class="post-metadata">

**Author:** ![Furtive\_Clown](https://avatars.discourse-cdn.com/v4/letter/f/3ec8ea/32.png) [@Furtive\_Clown](https://rubytalk.org/u/Furtive_Clown)\
**Post date:** [9 January 2008 17:35 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640/4 "2008-01-09T17:35:02Z")

</div>

Because using XML, YAML, or INI files robs me of the occasional #map,  
for instance, as shown in my example. The whole point is that I  
\*don't\* want configuration files that are little more than key/value  
pairs. Even if that was the case at the beginning of the project,  
eventually it becomes too restrictive as the project grows.

--FC

> **···**
>
> On Jan 9, 9:44 am, Karl von Laudermann \<doodpa...@mailinator.com\> wrote:
> 
> > If you just want to have configuration files that are little more than  
> > key/value pairs, why not use the Windows INI file format? There are  
> > already ruby gems that provide read/write access to INI files:

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://rubytalk.org/u/Dick_Davies)\
**Post date:** [10 January 2008 20:52 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640/5 "2008-01-10T20:52:37Z")

</div>

I used to love YAML but the whitespace / indent issue has bitten me  
too many times.

I needed a config file today, and eventually went for a  
lib/config.rb like:

CONFIG = {  
:servera =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;:hostname =\> 'a.com',  
&nbsp;&nbsp;&nbsp;&nbsp;:remote\_user =\> 'usera'  
},  
:serverb =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;:hostname =\> 'b.org',  
&nbsp;&nbsp;&nbsp;&nbsp;:remote\_user =\> 'userb'  
}  
}

I just load it in with 'require config'.

I know it's a bit cryptic to non-rubyists (compared to e.g. yaml)  
but it has the HUGE advantage of being easy to validate (ruby -wc  
lib/config.rb).

Be interested to know how others do it - an include/mixin maybe?

> **···**
>
> On Jan 9, 2008 9:00 AM, Marc Heiler \<shevegen@linuxmail.org\> wrote:
> 
> > Hmm the config style looks nice, but the code looks a bit complex?
> > 
> > I use yaml right now even though it has a few disadvantages (for me in  
> > this regard) because of indent (where a user may get parse errors).  
> > And from this yaml info I generate or build system info, on top of it.
> > 
> > I think what would be cool, would be a minimal "config" interpreter with  
> > only  
> > a very few (but still quite) readable lines of ruby code. (Anyway,  
> > that's just my opinion)
> 
> --  
> Rasputnik :: Jack of All Trades - Master of Nuns  
> [http://number9.hellooperator.net/](http://number9.hellooperator.net/)

---

<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:** [9 January 2008 18:25 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640/6 "2008-01-09T18:25:40Z")

</div>

I sometimes use the stupid simple:

#!/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\_\_

Here are the tests:

#!/usr/bin/env ruby -wKU

require "test/unit"  
require "tempfile"

require "config"

class TestConfig \< Test::Unit::TestCase  
&nbsp;&nbsp;&nbsp;def test\_config\_returns\_a\_customized\_ostruct  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_instance\_of(OpenStruct, config)  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_config\_object\_is\_passed\_into\_the\_file\_and\_used\_to\_set\_options  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = config(\<\<-END\_SETTINGS)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.string\_setting = "just a String"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.integer\_setting = 41  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END\_SETTINGS  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal("just a String", c.string\_setting)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(41, c.integer\_setting)  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def test\_exceptions\_bubble\_up\_to\_the\_caller  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_raise(RuntimeError) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config(\<\<-END\_ERROR)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Oops!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END\_ERROR  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;private

&nbsp;&nbsp;&nbsp;def config(content = String.new)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cf = Tempfile.new("ender\_config\_test")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cf \<\< content  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cf.flush  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Config.load\_config\_file(cf.path)  
&nbsp;&nbsp;&nbsp;end  
end

\_\_END\_\_

James Edward Gray II

> **···**
>
> On Jan 9, 2008, at 11:35 AM, furtive.clown@gmail.com wrote:
> 
> > On Jan 9, 9:44 am, Karl von Laudermann \<doodpa...@mailinator.com\> \> wrote:
> > 
> > > If you just want to have configuration files that are little more than  
> > > key/value pairs, why not use the Windows INI file format? There are  
> > > already ruby gems that provide read/write access to INI files:
> > 
> > Because using XML, YAML, or INI files robs me of the occasional #map,  
> > for instance, as shown in my example. The whole point is that I  
> > \*don't\* want configuration files that are little more than key/value  
> > pairs. Even if that was the case at the beginning of the project,  
> > eventually it becomes too restrictive as the project grows.

---

<div class="post-metadata">

**Author:** ![Naresh\_Ramaswamy](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@Naresh\_Ramaswamy](https://rubytalk.org/u/Naresh_Ramaswamy)\
**Post date:** [30 December 2008 08:59 UTC](https://rubytalk.org/t/using-ruby-for-config-files/43640/7 "2008-12-30T08:59:47Z")

</div>

hi,

I am new to Ruby and this thread was one of the solution I was looking  
for.

May be my question must be very basic ☹

Based on the Dick Davies comment I would like to use the config as  
described.  
But can someone let me know how to use the variables defined in this  
config file.

viz., I want to call the value of :hostname from another ruby file, how  
shall I do this?

Thanks

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