Java .properties file equivalent for Ruby? Java .properties file equivalent for Ruby? ruby ruby

Java .properties file equivalent for Ruby?


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


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]


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:     falsesources:- 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