Alex Rivera | Logout

Java .properties file equivalent for Ruby?

Asked 2008-09-19T00:36:12.310
28

I need to store some simple properties in a file and access them from Ruby.

I absolutely love the .properties file format that is the standard for such things in Java (using the java.util.Properties class)... it is simple, easy to use and easy to read.

So, is there a Ruby class somewhere that will let me load up some key value pairs from a file like that without a lot of effort?

I don't want to use XML, so please don't suggest REXML (my purpose does not warrant the "angle bracket tax").

I have considered rolling my own solution... it would probably be about 5-10 lines of code tops, but I would still rather use an existing library (if it is essentially a hash built from a file)... as that would bring it down to 1 line....


UPDATE: It's actually a straight Ruby app, not rails, but I think YAML will do nicely (it was in the back of my mind, but I had forgotten about it... have seen but never used as of yet), thanks everyone!

Edit
Report

2 Answers

10

Another option is to simply use another Ruby file as your configuration file.

Example, create a file called 'options'

{
    :blah   => 'blee',
    :foo    => 'bar',
    :items  => ['item1', 'item2'],
    :stuff  => true
}

And then in your Ruby code do something like:

ops = eval(File.open('options') {|f| f.read })
puts ops[:foo]
answered 2008-09-19T04:46:51.803
5

inifile - http://rubydoc.info/gems/inifile/2.0.2/frames will support basic .properties files and also .ini files with [SECTIONS] eg.

[SECTION]
key=value

YAML is good when your data has complex structure but can be fiddly with spaces, tabs, end of lines etc - which might cause problems if the files are not maintained by programmers. By contrast .properties and .ini files are more forgiving and may be suitable if you don't need the deep structure available through YAML.

answered 2013-03-25T03:41:15.890

Your Answer