Property files

Is there a library class for handling common property files that have
the key=value syntax? Basically all I need to do is read some
properties from a properties file, perform succ! on some of them, and
write the property file back to disk. There were a couple of threads
about properties files earlier in December that mentioned use of
Config.rb but I’m having trouble finding documentation for this library,
if it is one. I’m using Ruby 1.8.0.

Ron

---- “Ron Coutts” wrote: ----

Is there a library class for handling common property files that have
the key=value syntax?

There are several. I’m working on one:

http://ruby-talk.org/88419

···

John Long
www.wiseheartdesign.com

“Ron Coutts” rcoutts@envistatech.com schrieb im Newsbeitrag
news:000b01c3c99e$ee53f040$9e3c010a@envistatech.com

Is there a library class for handling common property files that have
the key=value syntax? Basically all I need to do is read some
properties from a properties file, perform succ! on some of them, and
write the property file back to disk. There were a couple of threads
about properties files earlier in December that mentioned use of
Config.rb but I’m having trouble finding documentation for this library,
if it is one. I’m using Ruby 1.8.0.

I made a posting some time ago, maybe that helps. The class can work with
nested names “foo.bar.baz=‘hello’”:

class Config
def initialize
@values = {}
end

def method_missing(sym,*args)
s = sym.to_s.freeze

if s[-1] == ?=
  # setter
  @values[s] = args.size == 1 ? args[0] : args
else
  # getter
  @values[s] ||= self.class.new
end

end

def load(file)
instance_eval File.readlines(file, nil).shift
end

def self.load(file)
conf = self.new
conf.load file
conf
end
end

conf = Config.new

conf.load “conf.rc”
p conf

conf = Config.load “conf.rc”
p conf

Cheers

robert

def method_missing(sym,*args)
s = sym.to_s.freeze

Is there a need to freeze s here? If I don’t freeze it, I can do this:

if s[-1] == ?=
  # setter
    s.sub!(/=$/, '')
  @values[s] = args.size == 1 ? args[0] : args
else

This, way, if the property file looks like

foo.name = ‘Foo’

I can access the value with

conf = Config.load “conf.rc”
puts conf.values[‘foo’].values[‘id’]

as opposed to

puts conf.values[‘foo’].values[‘id=’]

Regards,
Andre

···

On Fri, 2003-12-26 at 09:06, Robert Klemme wrote:

Hi all!

Is there a library class for handling common property files that have
the key=value syntax?

I have a simple one accessible through acceptor.tigris.org/.
Download the initial tarball, and grab acceptor/lib/runtimeProps.rb
for your work.

        Joseph Beckenbach
        lead XP tester, Eidogen Inc.

Well, i could also do

key = s.sub(/$=/, ‘’)
@values[key] = args.size == 1 ? args[0] : args

Anyway… is the freeze needed?

Andre

···

On Fri, 2003-12-26 at 09:45, Andre Nathan wrote:

Is there a need to freeze s here? If I don’t freeze it, I can do this:

“Andre Nathan” andre@digirati.com.br schrieb im Newsbeitrag
news:1072439266.2265.4.camel@drake.mz.digirati.com.br…

def method_missing(sym,*args)
s = sym.to_s.freeze

Is there a need to freeze s here?

unfrozen Strings are dup’ed when put into a hash. That’s why I put the
freeze there. But the freeze should be done later, when it’s clear whether
it’s a setter or a getter.

If I don’t freeze it, I can do this:

if s[-1] == ?=
  # setter
    s.sub!(/=$/, '')
  @values[s] = args.size == 1 ? args[0] : args
else

This, way, if the property file looks like

foo.name = ‘Foo’

I can access the value with

conf = Config.load “conf.rc”
puts conf.values[‘foo’].values[‘id’]

as opposed to

puts conf.values[‘foo’].values[‘id=’]

No. The idea of the whole thing is to be able to do

puts conf.foo.name

Cheers

robert
···

On Fri, 2003-12-26 at 09:06, Robert Klemme wrote: