Best practice for saving settings in separate file

  • Thread starter Oyvind Pettersen
  • Start date
O

Oyvind Pettersen

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
 
I

Intransition

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 =3D YAML.load(File.new("file.yaml"))

You can learn more about YAML here: http://www.yaml.org/
And very helpful to get started: http://www.yaml.org/YAML_for_ruby.html
 
J

Josh Cheek

On Wed, Apr 7, 2010 at 8:20 PM, Oyvind Pettersen <[email protected]=
m
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,

=D8yvind
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,
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 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 alway=
s
scratch that itch.
 
O

Oyvind Pettersen

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...)
 
M

Michael Fellinger

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

=C2=A0require 'yaml'

=C2=A0settings =3D YAML.load(File.new("file.yaml"))

# shorter, and makes sure the fd is closed too :)
require 'yaml'
settings =3D 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 =3D YAML::Store.new('file.yaml')
settings.transaction{|s| s['foo'] =3D 'bar' }
settings.transaction{|s| p s['foo'] }


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

--=20
Michael Fellinger
CTO, The Rubyists, LLC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top