On Mon, 19 Jul 2004, Edgardo Hames wrote: > Hi, everybody. I wondered how would you write a ResourceBundle-like > class in Ruby. Basically, what I need is hash which is initialized by > reading key,value pairs from a file. I've thought of this > > class ResourceBundle < Hash > def initialize(fileName) > #load data from fileName > end > end > > What do you think of this approach? > > Regards, > Ed use yaml: ~ > cat a.rb require 'yaml' class ResourceBundle < Hash def initialize path update(YAML::load(IO.read(path))) end end rb = ResourceBundle.new ARGV.shift p rb.keys p rb['answer'] ~ > cat a.cfg foo: bar key: value answer: 42 ~ > ruby a.rb a.cfg ["answer", "foo", "key"] 42 this also handle nesting for you: ~ > cat b.cfg foo: bar key: value answer: nested: 42 ~ > ruby a.rb b.cfg ["answer", "foo", "key"] {"nested"=>42} yaml is the light. ;-) -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================