tags:

views:

1686

answers:

5

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!

+11  A: 

Is this for a Rails application or a Ruby one?

Really with either you may be able to stick your properties in a yaml file and then YAML::Load(File.open("file")) it.


NOTE from Mike Stone: It would actually be better to do:

File.open("file") { |yf| YAML::load(yf) }

or

YAML.load_file("file")

as the ruby docs suggest, otherwise the file won't be closed till garbage collection, but good suggestion regardless :-)

Ryan Bigg
I edited inline as comments probably wouldn't be noticed...
Mike Stone
Yeah, that's a problem with SO the comments seem so... invisible. I didn't think about doing it in a block, brilliant!
Ryan Bigg
+1  A: 

Instead of the .properties style of config file, you might consider using YAML. YAML used in Ruby on Rails for database configuration, and has gained in popularity in other languages (Python, Java, Perl, and others).

An overview of the Ruby YAML module is here: http://www.ruby-doc.org/core/classes/YAML.html

And the home page of YAML is here: http://yaml.org

Christopher Currie
+1  A: 

Devender Gollapally wrote a class to do precisely that:

...though i'd recommend better to use a YAML file.

paradoja
+1  A: 

YAML will do it perfectly as described above. For an example, in one of my Ruby scripts I have a YAML file like:

migration:
  customer: Example Customer
  test:     false
sources:
- name:     Use the Source
  engine:   Foo
- name:     Sourcey
  engine:   Bar

which I then use within Ruby as:

config = YAML.load_file(File.join(File.dirname(__FILE__), ARGV[0]))
puts config['migration']['customer']

config['sources'].each do |source|
  puts source['name']
end
Dan Harper - Leopard CRM
+1  A: 

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]
Aaron Hinni
doesn't this option provide potential for abuse? eval will execute any arbitrary Ruby code such that, for example, nasty things could be done to the file system.
Straff
Abuse if you are evaling code from an untrusted source such as a web form, this answer is for a question regarding reading an options file. If someone wanted to tinker with that, they would also be in a position to tinker with the ruby code reading it...
Aaron Hinni