1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Constants in Ruby

By

Like just about every programming language every conceived, Ruby supports constants. A constant is a variable whose value cannot be changed. However, in Ruby, this has some caveats.

Constants in Ruby are any variable name that begins with a capital letter. This include variable names like FILENAME, but also variables like String. Wait, String? Yes, the class name String is a constant variable, as is every other class name, they're just constant variables that refer to the class instance. If it's not a keyword and it begins with a capital letter, it's very likely that it's a constant variable.

Constant variables are defined in the same way that any other variables are: simply by referring to them. However, since it's a warning (not an error) to change the value of a constant, you usually create them and assign to them in the same statement. Also, by convention, constant variable are in all capital letters. They can technically be a mix, but those are usually for referring to class names.


CONSTANT = 10
PI = 3.1415
G = 6.673E-11
PLANET = "Earth"

As was mentioned earlier, constants are not truly constant. In most languages, it's not even possible to change the value of a constant. In C, for example, constants are usually implemented with preprocessor symbols and don't even exist as a variable while the program is running. In C++, the language gives you no ability to modify a constant variable. In Ruby, constants are little more than a convention. If you try to modify a constant that's already been assigned to, you'll get a warning. More or less, Ruby sees them as no different from any other variable, and when you try to assign to any variable that begins in a capital letter and already exists, it prints a warning on standard error.


irb(main):001:0> PI = 3.1415
=> 3.1415
irb(main):002:0> PI = 3
(irb):2: warning: already initialized constant PI
=> 3
irb(main):003:0> puts PI
3
=> nil

Also, as mentioned before, class names are also constants. As such, they can be assigned to, changed, copied to other variables, etc. This behavior is not entirely expected by most programmers, but can be used for some simple tricks. For instance, you can create short aliases for long class names or classes nested deeply in modules. So if you're doing quite a lot of work with Net::HTTP, you can assign it to an easier to type alias.


require 'net/http'
H = Net::HTTP

H.get_print URI.parse('http://ruby.about.com/')

Another semi-useful feature of constants is that every module keeps a list of its constants. Not only will this give you a list of constant values defined, but a list of classes and modules also defined in that module. As a quick test, try the following code (and be prepared for a long list of constants).


puts Module.constants

The constants method returns an array of strings of all the constant names defined in that module. To access any of these constants, you can use the const_get method, like so.


# Instantiate a random class from a collection of
# classes in the module Bucket
rand = Bucket.const_get( Bucket.constants.shuffle.first ).new
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Variables
  6. Constants in Ruby

©2014 About.com. All rights reserved.