class Symbol
A Symbol object represents a named identifier inside the Ruby interpreter.
You can create a Symbol object explicitly with:
-
A symbol literal.
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.
Public Class Methods
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 155
def <=>: (Symbol object) -> (-1 | 0 | 1)
| (untyped) -> (-1 | 0 | 1)?
Compares self and other, using String#<=>.
Returns:
-
self.to_s <=> other.to_s, ifotheris a symbol. -
nil, otherwise.
Examples:
:bar <=> :foo # => -1 :foo <=> :foo # => 0 :foo <=> :bar # => 1 :foo <=> 'bar' # => nil
Class Symbol includes module Comparable, each of whose methods uses Symbol#<=> for comparison.
Related: String#<=>.
(untyped object) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 164
def ==: (untyped object) -> bool
Returns true if object is the same object as self, false otherwise.
(untyped object) → bool
Returns true if object is the same object as self, false otherwise.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 178
def =~: (Regexp regex) -> Integer?
| [T] (String::_MatchAgainst[String, T] object) -> T
Equivalent to symbol.to_s =~ object, including possible updates to global variables; see String#=~.
(int start, ?int length) → String?
(range[int?] range) → String?
(Regexp regexp, ?MatchData::capture backref) → String?
(String substring) → String?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 191
def []: (int start, ?int length) -> String?
| (range[int?] range) -> String?
| (Regexp regexp, ?MatchData::capture backref) -> String?
| (String substring) -> String?
Equivalent to symbol.to_s[]; see String#[].
# File vendor/bundle/ruby/4.0.0/gems/json-2.21.2/lib/json/add/symbol.rb, line 23 def as_json(*) { JSON.create_id => self.class.name, 's' => to_s, } end
Methods Symbol#as_json and Symbol.json_create may be used to serialize and deserialize a Symbol object; see Marshal.
Method Symbol#as_json serializes self, returning a 2-element hash representing self:
require 'json/add/symbol' x = :foo.as_json # => {"json_class"=>"Symbol", "s"=>"foo"}
Method JSON.create deserializes such a hash, returning a Symbol object:
Symbol.json_create(x) # => :foo
Methods Symbol#as_json and Symbol.json_create may be used to serialize and deserialize a Symbol object; see Marshal.
Method Symbol#as_json serializes self, returning a 2-element hash representing self:
require 'json/add/symbol' x = :foo.as_json # => {"json_class"=>"Symbol", "s"=>"foo"}
Method JSON.create deserializes such a hash, returning a Symbol object:
Symbol.json_create(x) # => :foo
() → Symbol
(:ascii | :lithuanian | :turkic) → Symbol
(:lithuanian, :turkic) → Symbol
(:turkic, :lithuanian) → Symbol
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 204
def capitalize: () -> Symbol
| (:ascii | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol
Equivalent to sym.to_s.capitalize.to_sym.
See String#capitalize.
(untyped object) → (-1 | 0 | 1)?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 241
def casecmp: (untyped object) -> (-1 | 0 | 1)?
Like Symbol#<=>, but case-insensitive; equivalent to self.to_s.casecmp(object.to_s):
lower = :abc upper = :ABC upper.casecmp(lower) # => 0 lower.casecmp(lower) # => 0 lower.casecmp(upper) # => 0
Returns nil if self and object have incompatible encodings, or if object is not a symbol:
sym = 'äöü'.encode("ISO-8859-1").to_sym other_sym = 'ÄÖÜ' sym.casecmp(other_sym) # => nil :foo.casecmp(2) # => nil
Unlike Symbol#casecmp?, case-insensitivity does not work for characters outside of ‘A’..‘Z’ and ‘a’..‘z’:
lower = :äöü upper = :ÄÖÜ upper.casecmp(lower) # => -1 lower.casecmp(lower) # => 0 lower.casecmp(upper) # => 1
Related: Symbol#casecmp?, String#casecmp.
(untyped object) → bool?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 274
def casecmp?: (untyped object) -> bool?
Returns true if self and object are equal after Unicode case folding, otherwise false:
lower = :abc upper = :ABC upper.casecmp?(lower) # => true lower.casecmp?(lower) # => true lower.casecmp?(upper) # => true
Returns nil if self and object have incompatible encodings, or if object is not a symbol:
sym = 'äöü'.encode("ISO-8859-1").to_sym other_sym = 'ÄÖÜ' sym.casecmp?(other_sym) # => nil :foo.casecmp?(2) # => nil
Unlike Symbol#casecmp, works for characters outside of ‘A’..‘Z’ and ‘a’..‘z’:
lower = :äöü upper = :ÄÖÜ upper.casecmp?(lower) # => true lower.casecmp?(lower) # => true lower.casecmp?(upper) # => true
Related: Symbol#casecmp, String#casecmp?.
() → Symbol
(:ascii | :fold | :lithuanian | :turkic) → Symbol
(:lithuanian, :turkic) → Symbol
(:turkic, :lithuanian) → Symbol
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 286
def downcase: () -> Symbol
| (:ascii | :fold | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 297
def empty?: () -> bool
Returns true if self is :'', false otherwise.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 305
def encoding: () -> Encoding
Equivalent to self.to_s.encoding; see String#encoding.
(*string suffixes) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 313
def end_with?: (*string suffixes) -> bool
Equivalent to self.to_s.end_with?; see String#end_with?.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 332
def inspect: () -> String
Returns a string representation of self (including the leading colon):
:foo.inspect # => ":foo"
Related: Symbol#to_s, Symbol#name.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 347
def length: () -> Integer
Equivalent to self.to_s.length; see String#length.
(Regexp | string pattern, ?int offset) → MatchData?
[T] (Regexp | string pattern, ?int offset) { (MatchData matchdata) → T } → T?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 357
def match: (Regexp | string pattern, ?int offset) -> MatchData?
| [T] (Regexp | string pattern, ?int offset) { (MatchData matchdata) -> T } -> T?
Equivalent to self.to_s.match, including possible updates to global variables; see String#match.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 366
def match?: (Regexp | string pattern, ?int offset) -> bool
Equivalent to sym.to_s.match?; see String#match.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 389
def name: () -> String
Returns a frozen string representation of self (not including the leading colon):
:foo.name # => "foo" :foo.name.frozen? # => true
Related: Symbol#to_s, Symbol#inspect.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 375
def next: () -> Symbol
(*Regexp | string prefixes) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 407
def start_with?: (*Regexp | string prefixes) -> bool
Equivalent to self.to_s.start_with?; see String#start_with?.
() → Symbol
(:ascii | :lithuanian | :turkic) → Symbol
(:lithuanian, :turkic) → Symbol
(:turkic, :lithuanian) → Symbol
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 429
def swapcase: () -> Symbol
| (:ascii | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol
Equivalent to sym.to_s.swapcase.to_sym.
See String#swapcase.
(?JSON::State? state) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/json-2.21.2/lib/json/add/symbol.rb, line 39 def to_json(state = nil, *a) state = ::JSON::State.from_state(state) if state.strict? super else as_json.to_json(state, *a) end end
Returns a JSON string representing self:
require 'json/add/symbol' puts :foo.to_json
Output:
# {"json_class":"Symbol","s":"foo"}
Returns a JSON string representing self:
require 'json/add/symbol' puts :foo.to_json
Output:
# {"json_class":"Symbol","s":"foo"}
Object#to_json
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 446
def to_proc: () -> Proc
Returns a Proc object which calls the method with name of self on the first parameter and passes the remaining parameters to the method.
proc = :to_s.to_proc # => #<Proc:0x000001afe0e48680(&:to_s) (lambda)> proc.call(1000) # => "1000" proc.call(1000, 16) # => "3e8" (1..3).collect(&:to_s) # => ["1", "2", "3"]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 458
def to_s: () -> String
Returns a string representation of self (not including the leading colon):
:foo.to_s # => "foo"
Related: Symbol#inspect, Symbol#name.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 468
def to_sym: () -> self
Returns self.
Related: String#to_sym.
() → Symbol
(:ascii | :lithuanian | :turkic) → Symbol
(:lithuanian, :turkic) → Symbol
(:turkic, :lithuanian) → Symbol
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/symbol.rbs, line 478
def upcase: () -> Symbol
| (:ascii | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol
Equivalent to sym.to_s.upcase.to_sym.
See String#upcase.