# Best practice for saving settings in separate file

**URL:** <https://www.ruby-forum.com/t/best-practice-for-saving-settings-in-separate-file/186506>\
**Category:** Ruby\
**Created:** [April 8, 2010, 3:20am UTC](https://www.ruby-forum.com/t/best-practice-for-saving-settings-in-separate-file/186506 "2010-04-08T03:20:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![opet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f4b2a3/32.png) [@opet](https://www.ruby-forum.com/u/opet)\
**Post date:** [April 8, 2010, 3:20am UTC](https://www.ruby-forum.com/t/best-practice-for-saving-settings-in-separate-file/186506/1 "2010-04-08T03:20:48Z")

</div>

I’m currently building an application that relies on a rather large set  
of settings and paths that needs to sit in its own file.

What are the best way to go about? XML? Simple strings in a text file?

The settings files would be created manually, but the app will have to  
update a few integers in the files.

Thanks in advance,

Ã˜yvind

---

<div class="post-metadata">

**Author:** ![opet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f4b2a3/32.png) [@opet](https://www.ruby-forum.com/u/opet)\
**Post date:** [April 8, 2010, 4:15am UTC](https://www.ruby-forum.com/t/best-practice-for-saving-settings-in-separate-file/186506/2 "2010-04-08T04:15:24Z")

</div>

On Apr 7, 9:20 pm, Oyvind P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m currently building an application that relies on a rather large set  
> of settings and paths that needs to sit in its own file.
> 
> What are the best way to go about? XML? Simple strings in a text file?
> 
> The settings files would be created manually, but the app will have to  
> update a few integers in the files.

Unless you have a reason not to use it, the most common practice in  
Rubydom is to use YAML.

require ‘yaml’

settings = YAML.load(File.new(“file.yaml”))

You can learn more about YAML here: [http://www.yaml.org/](http://www.yaml.org/)  
And very helpful to get started: [YAML.rb is YAML for Ruby | Cookbook](http://www.yaml.org/YAML_for_ruby.html)

---

<div class="post-metadata">

**Author:** ![opet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f4b2a3/32.png) [@opet](https://www.ruby-forum.com/u/opet)\
**Post date:** [April 8, 2010, 4:45am UTC](https://www.ruby-forum.com/t/best-practice-for-saving-settings-in-separate-file/186506/3 "2010-04-08T04:45:51Z")

</div>

On Wed, Apr 7, 2010 at 8:20 PM, Oyvind P.  
\<removed\_email\_address@domain.invalid

> wrote:

> ## Ã˜yvind
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I think the book Ruby in Practice has a section on this (I left it at  
work,  
so I can’t check for you, sorry).

I was looking at Configliere the other day, and it looks pretty cool,  
[GitHub - mrflip/configliere: Wise, discreet configuration for ruby scripts: integrate config files, environment variables and command line with no fuss](http://github.com/mrflip/configliere) It looks like you can use it to  
take  
the place of optparse too (at least thats how I interpreted it).

You might also check out [GitHub - binarylogic/settingslogic: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.](http://github.com/binarylogic/settingslogic)  
which  
is by binarylogic, the author of AuthLogic (a rather popular  
authentication  
gem for Rails). Looking through the tests and source, though, it doesn’t  
appear to have a way to save the settings back to the file. It uses  
Yaml,  
like Intransition suggested. But it’s only about 150 lines, you could  
always  
scratch that itch.

---

<div class="post-metadata">

**Author:** ![opet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f4b2a3/32.png) [@opet](https://www.ruby-forum.com/u/opet)\
**Post date:** [April 8, 2010, 4:49pm UTC](https://www.ruby-forum.com/t/best-practice-for-saving-settings-in-separate-file/186506/4 "2010-04-08T16:49:40Z")

</div>

Thanks a lot!

I had a quick look at YAML, and it seems to do what I need it to do.

Thanks!

(Now I just need to figure out my other issue…)

---

<div class="post-metadata">

**Author:** ![opet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f4b2a3/32.png) [@opet](https://www.ruby-forum.com/u/opet)\
**Post date:** [April 9, 2010, 7:59am UTC](https://www.ruby-forum.com/t/best-practice-for-saving-settings-in-separate-file/186506/5 "2010-04-09T07:59:47Z")

</div>

On Thu, Apr 8, 2010 at 11:12 AM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > update a few integers in the files.
> 
> Unless you have a reason not to use it, the most common practice in  
> Rubydom is to use YAML.
> 
> Â require ‘yaml’
> 
> Â settings = YAML.load(File.new(“file.yaml”))

# shorter, and makes sure the fd is closed too 🙂

require ‘yaml’  
settings = YAML.load\_file(‘file.yaml’)

# longer, but better if you want to read and write the file from

# multiple processes simultaneously on unix

require ‘yaml/store’  
settings = YAML::Store.new(‘file.yaml’)  
settings.transaction{|s| s[‘foo’] = ‘bar’ }  
settings.transaction{|s| p s[‘foo’] }

btw, anyone wanna add JSON::Store to ruby core?
