Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constant values in Rails

Tags:

I have some data that I want to store somewhere in my Rails app because I use it for generating form fields, checking a submitted form to ensure its values are valid, etc. Basically, I want the data in one location because I make use of it in several places.

Previously, I was defining an initialize method in my controller and initializing instance variables within that method, e.g. @graph_types = ['bar', 'line']. This seemed a bad idea because that's really all initialize was being used for (initializing those values) and the instance variables could be changed later, which I don't want.

Now, I define constants outside of any method in my controller, right up at the top after my filters, and I freeze them, e.g. GraphTypes = ['bar', 'line'].freeze.

I didn't want to store such data in a config file because then I would have to keep track of an extra file, read in the file and parse it, etc. I didn't want to store this data in the database because that seems like overkill; I don't need to do any crazy LEFT OUTER JOIN-type queries combining available graph types with another of my constants, say Themes = ['Keynote', 'Odeo', '37 Signals', 'Rails Keynote'].freeze. I didn't want to store the data in environment.rb because this data only pertains to a particular controller.

Considering all this, am I going about this 'the Ruby way'?

like image 276
Sarah Vessels Avatar asked Dec 11 '08 22:12

Sarah Vessels


People also ask

What are constants in Ruby?

What is a constant in Ruby? A constant is a type of variable which always starts with a capital letter. They can only be defined outside of methods, unless you use metaprogramming. Constants are used for values that aren't supposed to change, but Ruby doesn't prevent you from changing them.

How do you find the constants in Ruby?

Ruby Constants Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally. Constants may not be defined within methods.

Are constants global in Ruby?

Although constants look like local variables with capital letters, they have the visibility of global variables: they can be used anywhere in a Ruby program without regard to scope.

Do not define constants this way within a block rails?

From the docs: Do not define constants within a block, since the block's scope does not isolate or namespace the constant in any way. If you are trying to define that constant once, define it outside of the block instead, or use a variable or method if defining the constant in the outer scope would be problematic.


1 Answers

For constants that don't really belong anywhere else I have a StaticData class.

  class StaticData      GRAPH_TYPES = ['bar', 'line']      SOMETHING_ELSE = ['A', 'B']    end 

Then I get at it with

StaticData::GRAPH_TYPES 
like image 179
user37011 Avatar answered Oct 11 '22 14:10

user37011