Best practice for saving settings in separate file

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

On Apr 7, 9:20 pm, Oyvind P. [email protected]
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/
And very helpful to get started: YAML.rb is YAML for Ruby | Cookbook

On Wed, Apr 7, 2010 at 8:20 PM, Oyvind P.
<[email protected]

wrote:

Øyvind

Posted via 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 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.
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.

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

On Thu, Apr 8, 2010 at 11:12 AM, Intransition [email protected]
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 :slight_smile:

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?