class Symbol

A Symbol object represents a named identifier inside the Ruby interpreter.

You can create a Symbol object explicitly with:

The same Symbol object will be created for a given name or string for the duration of a program’s execution, regardless of the context or meaning of that name. Thus if Fred is a constant in one context, a method in another, and a class in a third, the Symbol :Fred will be the same object in all three contexts.

module One
  class Fred
  end
  $f1 = :Fred
end
module Two
  Fred = 1
  $f2 = :Fred
end
def Fred()
end
$f3 = :Fred
$f1.object_id   #=> 2514190
$f2.object_id   #=> 2514190
$f3.object_id   #=> 2514190

Constant, method, and variable names are returned as symbols:

module One
  Two = 2
  def three; 3 end
  @four = 4
  @@five = 5
  $six = 6
end
seven = 7

One.constants
# => [:Two]
One.instance_methods(true)
# => [:three]
One.instance_variables
# => [:@four]
One.class_variables
# => [:@@five]
global_variables.grep(/six/)
# => [:$six]
local_variables
# => [:seven]

A Symbol object differs from a String object in that a Symbol object represents an identifier, while a String object represents text or data.

What’s Here

First, what’s elsewhere. Class Symbol:

Here, class Symbol provides methods that are useful for:

Methods for Querying

Methods for Comparing

Methods for Converting