Is there a class in the ruby lib that is equivalent to Properties class
in Java?
On 13.12.2006 13:59, Olivia D. wrote:
Is there a class in the ruby lib that is equivalent to Properties class
in Java?
Hash.
robert
Olivia D. wrote:
Is there a class in the ruby lib that is equivalent to Properties class
in Java?
If you can read something that can parse and emit Java .properties
files, then no, not in standard ruby, and a quick google doesn’t find
much.
You can hack something together…
IO.readlines(‘some.properties’).reject{|l|l=~/^\s*#/}.
map{|l|l.split(’=’,2)}
But I’m sure that misses a whole buncha edge cases, like backslash
escaping, and Ant-style property substitution.
If you mean some way to parse/emit key/value parse from/to a
human-readable format… Hash and YAML.
Devin
On Dec 13, 2006, at 6:59 AM, Olivia D. wrote:
Is there a class in the ruby lib that is equivalent to Properties
class
in Java?
I think the following code should cover most of the Properties class
features:
#!/usr/bin/env ruby -w
require “forwardable”
require “yaml”
class Properties
def self.load(file_path)
File.open(file_path) { |file| YAML.load(file) }
end
def initialize
@properties = Hash.new
@defaults = nil
end
attr_accessor :defaults
extend Forwardable
def_delegator :@properties, :[]=
def
if @defaults and not @properties.include? property
@defaults[property]
else
@properties[property]
end
end
def save(file_path)
File.open(file_path, “w”) { |file| YAML.dump(self, file) }
end
end
END
Hope that helps.
James Edward G. II