[Rails] Configuration values

Demetrius Nunes demetrius at interface-ti.com.br
Wed Jan 5 03:05:18 GMT 2005


Marcel Molina Jr. wrote:

>>The thing that makes this configuration data structure available to you need
>>to be a method, per se. You could alternatively make it a CONSTANT.
>>
>>e.g
>>
>>    APP_CONFIG = YAML.load(File.open(RAILS_ROOT + '/config/development.yml'))
>>
>>FYI you can't name it Config though as that clashes with a constant set in
>>TMail. I'd just stick with a method in any event.
>>    
>>
>Though one benefit of the constant is that the file just gets loaded
>once per request rather than once per config value access.
>
>marcel
>  
>
You can use a "cache pattern" to address this, like in:

def app_config
    return @app_config if @app_config
    @app_config = YAML.load(...)
    @app_config
end

Is there a better ruby way to write this kind of pattern?



More information about the Rails mailing list