[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is there a class that is equivalent to Properties in Java

Olivia Dou

12/13/2006 12:59:00 PM

Is there a class in the ruby lib that is equivalent to Properties class
in Java?

--
Posted via http://www.ruby-....

3 Answers

Robert Klemme

12/13/2006 1:49:00 PM

0

On 13.12.2006 13:59, Olivia Dou wrote:
> Is there a class in the ruby lib that is equivalent to Properties class
> in Java?

Hash.

robert

Devin Mullins

12/13/2006 2:21:00 PM

0

Olivia Dou 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

James Gray

12/13/2006 2:45:00 PM

0

On Dec 13, 2006, at 6:59 AM, Olivia Dou 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 [](property)
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 Gray II