# Is there a class that is equivalent to Properties in Java

**URL:** <https://www.ruby-forum.com/t/is-there-a-class-that-is-equivalent-to-properties-in-java/79227>\
**Category:** Ruby\
**Created:** [December 13, 2006, 1:59pm UTC](https://www.ruby-forum.com/t/is-there-a-class-that-is-equivalent-to-properties-in-java/79227 "2006-12-13T13:59:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliviabean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/df705f/32.png) [@oliviabean](https://www.ruby-forum.com/u/oliviabean)\
**Post date:** [December 13, 2006, 1:59pm UTC](https://www.ruby-forum.com/t/is-there-a-class-that-is-equivalent-to-properties-in-java/79227/1 "2006-12-13T13:59:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oliviabean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/df705f/32.png) [@oliviabean](https://www.ruby-forum.com/u/oliviabean)\
**Post date:** [December 13, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/is-there-a-class-that-is-equivalent-to-properties-in-java/79227/2 "2006-12-13T14:50:22Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![oliviabean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/df705f/32.png) [@oliviabean](https://www.ruby-forum.com/u/oliviabean)\
**Post date:** [December 13, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/is-there-a-class-that-is-equivalent-to-properties-in-java/79227/3 "2006-12-13T15:22:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oliviabean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/df705f/32.png) [@oliviabean](https://www.ruby-forum.com/u/oliviabean)\
**Post date:** [December 13, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/is-there-a-class-that-is-equivalent-to-properties-in-java/79227/4 "2006-12-13T15:46:36Z")

</div>

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
