From: Intransition Date: 2009-11-25T21:30:12+09:00 Subject: Re: Where should I put my config file, and how to load it? On Nov 24, 11:33 am, Ian Bridgeman wrote: > Hello all! > > I have a small app/set of libraries I'm developing at work called > Sweet. I'm distributing it internally as a ruby gem, which is working > nicely. However I rather foolishly put Sweet's yaml config file in its > config/ directory, so now every time the gem gets upgraded, it wipes > out the current settings. Oops! My plan to fix this problem is to > store the config file at ~/.sweetrc, with a global config file at > /etc/sweetrc to fall back to if the home directory one isn't > available. Does this sound like a sensible solution? Or am I going > mad? Please, don't use ~/.sweetrc. Be cool and kind and use XDG standard. http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html http://xdg.rubyforge.org/ > The second (and more ruby-ish) question I have is: How can I make sure > all my classes have access to the contents of the config file? My > classes all reside within the 'Sweet' module, and ideally I would like > to be able to do something along the lines of > > @data_directory = Sweet::Config['data_directory'] Off the top of my head: class Sweet::Config DEFAULTS = {... } def initialize file = XDG.config_find('sweet/config.yaml') data = file ? YAML.load(File.new(file)) : {} @data = DEFAULTS.merge(data) end def self.global @global ||= new end def self.[](key) global[key.to_s] end end