On this page
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:
-
Inherits from class Object.
-
Includes module Comparable.
Here, class Symbol provides methods that are useful for:
Methods for Querying
-
::all_symbols: Returns an array of the symbols currently in Ruby’s symbol table. -
=~: Returns the index of the first substring in symbol that matches a givenRegexpor other object; returnsnilif no match is found. -
[],slice: Returns a substring of symbol determined by a given index, start/length, or range, or string. -
empty?: Returnstrueifself.lengthis zero;falseotherwise. -
encoding: Returns theEncodingobject that represents the encoding of symbol. -
end_with?: Returnstrueif symbol ends with any of the given strings. -
match: Returns aMatchDataobject if symbol matches a givenRegexp;nilotherwise. -
match?: Returnstrueif symbol matches a givenRegexp;falseotherwise. -
start_with?: Returnstrueif symbol starts with any of the given strings.
Methods for Comparing
-
<=>: Returns -1, 0, or 1 as a given symbol is smaller than, equal to, or larger than symbol. -
==,===: Returnstrueif a given symbol has the same content and encoding. -
casecmp: Ignoring case, returns -1, 0, or 1 as a given symbol is smaller than, equal to, or larger than symbol. -
casecmp?: Returnstrueif symbol is equal to a given symbol after Unicode case folding;falseotherwise.
Methods for Converting
-
capitalize: Returns symbol with the first character upcased and all other characters downcased. -
downcase: Returns symbol with all characters downcased. -
inspect: Returns the string representation ofselfas a symbol literal. -
name: Returns the frozen string corresponding to symbol. -
succ,next: Returns the symbol that is the successor to symbol. -
swapcase: Returns symbol with all upcase characters downcased and all downcase characters upcased. -
to_proc: Returns aProcobject which responds to the method named by symbol. -
upcase: Returns symbol with all characters upcased.
Class methods 2
Instance methods 31
Ruby version changes 3
-
3.4
-
The string returned by
Symbol#to_snow emits a deprecation warning when mutated, and will be frozen in a future version of Ruby. These warnings can be enabled with-W:deprecatedor by settingWarning[:deprecated] = true. [Feature #20350]
-
-
3.0
-
Symbol#to_procnow returns a lambdaProc. [Feature #16260] -
Symbol#namehas been added, which returns the name of the symbol if it is named. The returned string is frozen. [Feature #16150]
-