# Using configuration files (replacing shellscripts)

**URL:** <https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232>\
**Category:** Ruby\
**Created:** [October 6, 2006, 9:50am UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232 "2006-10-06T09:50:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@Karl\_V](https://www.ruby-forum.com/u/Karl_V)\
**Post date:** [October 6, 2006, 9:50am UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232/1 "2006-10-06T09:50:49Z")

</div>

Hi!

I want to use configuration files in order to save values, directories,  
and files. I did some googeling to find out that the ruby community  
often suggests ruby-files (hashes) to save configurations. These  
rb-files are read in by interpreting. (btw, anyone suggests a cool  
part of code for this with errorhandling and so on?)

But I do have some problems with that method:

,----[shellscript configuration example]  
| export value1=42  
| export value2=23  
| export path1=/some/where  
| export path2=$path1/else  
| export file1=$path1/foo  
| export file2=$path2/bar  
`----

First, I tried to replace it 1:1 using ruby but failed when it came to  
reusing values:

,----[woun’t run]  
| $config = {  
| ‘value1’ =\> ‘42’,  
| ‘value2’ =\> ‘23’,  
| ‘path1’ =\> ‘/some/where’,  
| ‘path2’ =\> $config[‘path1’]+’/else’,  
| […]  
| }  
`----

Is there a way to accomplish this in another way? (using self or  
something like that)

Meanwhile I did a workaround:

,----[Workaround]  
| $directories = {  
| ‘path1’ =\> ‘/some/where’,  
| ‘path2’ =\> ‘/some/where/else’, # redundancy! ☹  
| […]  
| }  
|  
| $files = {  
| ‘file1’ = $directories[‘path1’]+’/foo’, # problem!  
| […]  
| }  
|  
| $values = { … }  
`----

This did not work out either. How can I solve my problem?

---

<div class="post-metadata">

**Author:** ![Karl\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@Karl\_V](https://www.ruby-forum.com/u/Karl_V)\
**Post date:** [October 6, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232/2 "2006-10-06T13:41:12Z")

</div>

Hi…

Karl V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I want to use configuration files in order to save values,  
> directories, and files. I did some googeling to find out that the  
> ruby community often suggests ruby-files (hashes) to save  
> configurations. These rb-files are read in by interpreting. (btw,  
> anyone suggests a cool part of code for this with errorhandling and  
> so on?)

If I understand what you’re asking for, you may want to have a look at  
YAML. Your config file can look like this:

#### 

adapter: mysql  
database: demodb  
username: karl  
password: pa$$w0rd  
socket: /var/lib/mysql/mysql.sock

#### 

Turning that file into a ruby hash is a one-liner…

require ‘yaml’  
dbconfig = YAML::load(IO.read(‘config.yml’))  
assert\_equal(‘karl’, dbconfig[:username])

Jim

---

<div class="post-metadata">

**Author:** ![Karl\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@Karl\_V](https://www.ruby-forum.com/u/Karl_V)\
**Post date:** [October 6, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232/3 "2006-10-06T14:07:15Z")

</div>

- Jim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi…

Hi!

> Turning that file into a ruby hash is a one-liner…

Thanks for the hint.

Is is possible to reduce the redundancy of

,----  
| basedir: /this/directory  
| logdir: /this/directory/log  
`----

to something like that?

,----  
| basedir: /this/directory  
| logdir: $basedir/log  
`----

I have to use this in the configs quite often.

TNX!

---

<div class="post-metadata">

**Author:** ![Karl\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@Karl\_V](https://www.ruby-forum.com/u/Karl_V)\
**Post date:** [October 6, 2006, 2:51pm UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232/4 "2006-10-06T14:51:45Z")

</div>

Karl V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

[…]

> | basedir: /this/directory  
> | logdir: $basedir/log  
> `----

I’m not sure about that. YAML has the concept of “aliases” and  
“anchors” which are close to what you’re asking for, but I’m not sure  
it would be possible to concatenate an anchor with a plain string.  
Perhaps someone more YAML-savvy will chime in.

Sorry,  
Jim

---

<div class="post-metadata">

**Author:** ![Karl\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@Karl\_V](https://www.ruby-forum.com/u/Karl_V)\
**Post date:** [October 6, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232/5 "2006-10-06T15:41:58Z")

</div>

Karl V. wrote:

> > database: demodb
> 
> `----
> 
> I have to use this in the configs quite often.

I’ve been asking for interpolation in YAML for a while now. Either the  
feature or I am being ignored ☹

FYI, though. It’s not to hard to create a two-pass filter to add this  
functionity if you really need it.

T.

---

<div class="post-metadata">

**Author:** ![Karl\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@Karl\_V](https://www.ruby-forum.com/u/Karl_V)\
**Post date:** [October 6, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232/6 "2006-10-06T16:06:02Z")

</div>

On Fri, 6 Oct 2006, Trans wrote:

> > | basedir: /this/directory  
> > | logdir: $basedir/log  
> > `----
> > 
> > I have to use this in the configs quite often.

harp:~ \> cat a.rb

# this would come from file

conf = \<\<‘conf’

basedir “/this/directory”

logdir “#{ basedir }/log”

conf

require ‘traits’  
class Config \< OpenTraits  
def initialize(conf) instance\_eval conf end  
end

config = Config.new conf

p config.basedir  
p config.logdir

harp:~ \> ruby a.rb  
“/this/directory”  
“/this/directory/log”

-a

---

<div class="post-metadata">

**Author:** ![Karl\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@Karl\_V](https://www.ruby-forum.com/u/Karl_V)\
**Post date:** [October 10, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/using-configuration-files-replacing-shellscripts/71232/7 "2006-10-10T22:01:02Z")

</div>

Jim C. wrote:

> Turning that file into a ruby hash is a one-liner…
> 
> require ‘yaml’  
> dbconfig = YAML::load(IO.read(‘config.yml’))  
> assert\_equal(‘karl’, dbconfig[:username])
> 
> Jim

I wrote something a bit more general. It assumes that the yaml file  
serializes a Ruby hash. You’d probably want this to be a singleton  
class, but my uses don’t require that. Perhaps the OP would find it  
useful:

class Parameters

require ‘yaml’  
def initialize(config\_file)  
@config = YAML.load(File.open(config\_file))

```
# This creates a global variable for each entry in the yaml

```

configuration file.  
# I recommend that this interface be deprecated in favor of using  
accessor syntax.  
# E.g. creating a Parameters object config = Parameters.new and  
calling accessors  
# on config.

```
@config.keys.each do |parameter|
  eval('$' + "#{parameter} = @config[parameter]")
end

# This creates an accessor for each of the configuration entries in

```

the yaml  
# configuration file.

```
@config.keys.each do |parameter|
  eval("@#{parameter} = @config[parameter]")
  self.class.class_eval {attr_reader parameter}
end

```

end  
end
