Ruby 2 Constant

A Ruby constant is like a variable, except that its value is supposed to remain constant for the duration of the program. The Ruby interpreter does not actually enforce the constancy of constants, but it does issue a warning if a program changes the value of a constant (as shown in this trivial example) - p054constwarn.rb
  1. # p054constwarn.rb  
  2. A_CONST = 10  
  3. A_CONST = 20  
Produces a warning:
  1. p054constwarn.rb:3: warning: already initialized constant A_CONST  
Lexically, the names of constants look like the names of local variables, except that they begin with a capital letter. By convention, most constants are written in all uppercase with underscores to separate words, LIKE_THIS. Ruby class and module names are also constants, but they are conventionally written using initial capital letters and camel case, LikeThis.
Note that constants do not exist until a value is actually assigned to them.
Although constants should not be changed, you can modify the internal states of the objects they reference, as seen in p055constalter.rb
  1. # p055constalter.rb  
  2. A_CONST = "Doshi"  
  3. B_CONST = A_CONST  
  4. A_CONST[0] = "J" # alter string referenced by constant  
  5. puts A_CONST # displays Joshi  
  6. puts B_CONST # also displays Joshi  
IN RAILS: You can find examples of this kind of operation (modify) in the Rails source code, where constants figure prominently and the objects they represent undergo fairly frequent changes.
Note:
  • Constants defined within a class or module may be accessed anywhere within the class or module.
  • Outside the class or module, they may be accessed using the scope operator, :: prefixed by an expression that returns the appropriate class or module.
  • Constants defined outside any class or module may be accessed as it is or by using the scope operator with no prefix.
  • Constants may not be defined in methods.
  • Constants may be added to existing classes and modules from the outside by using the class or module name and the scope operator before the constant name.
The program p056const.rb shows all of this:
  1. OUTER_CONST = 99  
  2.   
  3. class Const  
  4.     def get_const  
  5.       CONST  
  6.     end  
  7.     CONST = OUTER_CONST + 1  
  8. end  
  9.   
  10. puts Const.new.get_const  
  11. puts Const::CONST  
  12. puts ::OUTER_CONST  
  13. puts Const::NEW_CONST = 123  
Another elaborate example on own methods in a class is p057mymethods2.rb In this example we shall also see how to write a class method.
  1. # p057mymethods2.rb  
  2. # variables and methods start lowercase  
  3. $glob = 5             # global variables start with $  
  4. class TestVar         # class name constant, start uppercase  
  5.  @@cla = 6            # class variables start with @@  
  6.  CONST_VAL = 7        # constant style, all caps, underscore  
  7.  def initialize(x)    # constructor  
  8.   @inst = x           # instance variables start with @  
  9.   @@cla += 1          # each object shares @@cla  
  10.  end  
  11.  def self.cla         # class method, getter  
  12.   @@cla  
  13.  end  
  14.  def self.cla=(y)     # class method, setter, also TestVar.  
  15.   @@cla = y  
  16.  end  
  17.  def inst             # instance method, getter  
  18.   @inst  
  19.  end  
  20.  def inst=(i)         # instance method, setter  
  21.   @inst = i  
  22.  end  
  23. end  
  24. puts $glob  
  25. test = TestVar.new(3) # calls constructor  
  26. puts TestVar.cla      # calls getter  
  27. puts test.inspect     # gives object ID and instance vars  
  28. TestVar.cla = 4       # calls setter  
  29. test.inst=8           # calls setter  
  30. puts TestVar.cla  
  31. puts test.inst        # calls getter  
  32. other = TestVar.new(17)  
  33. puts other.inspect  
  34. puts TestVar.cla