I need to be able to access a list throughout my Rails project. For simplicity’s sake, this is the list I put in environment.rb
FOODS = ['CHEESE', 'EGGS', 'ORANGES']
I want every file to have access to this constant, but at the same time I’m worried that some file might mess up the list for all the other files by doing something that only it particularly needs, e.g.: FOODS.push("TEMPORARY_FOOD")
What’s the right way to make this list globally accessible? Should I be using .freeze, or should I be using some other method than environment.rb.
A ruby constant by convention is not meant to be modified. Using
.freezeis fine, but remember that it does not stop the constant from being redefined,The constant should already be accessible globally if you defined it in
environment.rb. A better place to put the constant initialization in a file in config/initializers – unless you need it in environment.rb – makes upgrading environment.rb (Rails upgrade easier)