A relatively simple way to load some configuration once and safely, is through the use of the Singleton module provided with Ruby.
class FooConfig include Singleton attr_reader :foo def initialize() @foo = true end end
Accessing the instance is easy, call
FooConfig.instance. This method is also thread-safe so no two threads are going to create/access a different instance. The example is trite, but imagine instead loading data from a remote service. Now combine this with Mike Perham’s Configuration for Rails, the Right Way.
MyApp::Application.config.foo = FooConfig.instance
There is really little reason to access the configuration via Rails.application.config.foo, except that this way you can change it for things like tests more easily.
Speaking of testing, if you find you need to reset a singleton for some reason, the following seems to do the trick, which comes from http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby. Just paste into a file like spec/support/singleton_reset.rb (if using rspec) and require it in spec/spec_helper.rb
require 'singleton' class <<Singleton def included_with_reset(klass) included_without_reset(klass) class <<klass def reset_instance Singleton.send :__init__, self self end end end alias_method :included_without_reset, :included alias_method :included, :included_with_reset end