: 
http://rubylearning.com/
http://rubylearning.com/satishtalim/tutorial.html
http://rubylearning.com/download/downloads.html
http://rubylearning.com/other/certification.html
http://rubylearning.com/blog/
http://rubylearning.com/other/ruby_news.html
http://rubylearning.com/other/testimonials.html
http://rubylearning.com/jobs/ruby_jobs.html
http://rubylearning.com/other/ruby_gurus.html
http://rubylearning.com/satishtalim/services.html
http://rubylearning.com/contact/contact.html
http://rubylearning.com/satishtalim/about.html


Constants

A Ruby constant is a reference to an object. Constants are created when they are first assigned to (as per the rules below). In the current implementation of Ruby, reassignment of a constant generates a warning but not an error as shown in this trivial example - p054constwarn.rb


# p054constwarn.rb

A_CONST = 10

A_CONST = 20


Produces a warning:


p054constwarn.rb:3: warning: already initialized constant A_CONST

Although constants should not be changed, you can modify the internal states of the objects they reference, as seen in p055constalter.rb


# p055constalter.rb

A_CONST = "Doshi"

B_CONST = A_CONST

A_CONST[0] = "J" # alter string referenced by constant

puts A_CONST # displays Joshi

puts B_CONST # also displays Joshi


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:

# p056const.rb

OUTER_CONST = 99

class Const

  def get_const

    CONST

  end

  CONST = OUTER_CONST + 1

end

puts Const.new.get_const

puts Const::CONST

puts ::OUTER_CONST

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.


# variables and methods start lowercase

$glob = 5             # global variables start with $

class TestVar         # class name constant, start uppercase

 @@cla = 6            # class variables start with @@

 CONST_VAL = 7        # constant style, all caps, underscore

 def initialize(x)   

   @inst = x          # instance variables start with @

   @@cla += 1         # each object shares @@cla

 end

 def self.cla         # class method, getter

   @@cla

 end

 def self.cla=(y)     # class method, setter, also TestVar.

  @@cla = y

 end

 def inst             # instance method, getter

  @inst

 end

 def inst=(i)         # instance method, setter

  @inst = i

 end

end

puts $glob

test = TestVar.new(3)

puts TestVar.cla      # calls getter

puts test.inspect     # gives object ID and instance vars

TestVar.cla = 4       # calls setter

test.inst=8           # calls setter

puts TestVar.cla

puts test.inst        # calls getter

other = TestVar.new(17)

puts other.inspect

puts TestVar.cla

 


Learning Ruby Navigation                                                                 <Object Serialization  | TOC  |  Modules/Mixins>