# # Symbol objects represent named identifiers inside the Ruby interpreter. # # You can create a Symbol object explicitly with: # # * A [symbol literal](rdoc-ref:syntax/literals.rdoc@Symbol+Literals). # # # 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] # # Symbol objects are different from String objects in that Symbol objects # represent identifiers, while String objects represent text or data. # # ## What's Here # # First, what's elsewhere. Class Symbol: # # * Inherits from [class Object](rdoc-ref:Object@What-27s+Here). # * Includes [module Comparable](rdoc-ref:Comparable@What-27s+Here). # # # Here, class Symbol provides methods that are useful for: # # * [Querying](rdoc-ref:Symbol@Methods+for+Querying) # * [Comparing](rdoc-ref:Symbol@Methods+for+Comparing) # * [Converting](rdoc-ref:Symbol@Methods+for+Converting) # # # ### 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 # given Regexp or other object; returns `nil` if no match is found. # * #[], #slice : Returns a substring of symbol determined by a given index, # start/length, or range, or string. # * #empty?: Returns `true` if `self.length` is zero; `false` otherwise. # * #encoding: Returns the Encoding object that represents the encoding of # symbol. # * #end_with?: Returns `true` if symbol ends with any of the given strings. # * #match: Returns a MatchData object if symbol matches a given Regexp; `nil` # otherwise. # * #match?: Returns `true` if symbol matches a given Regexp; `false` # otherwise. # * #length, #size: Returns the number of characters in symbol. # * #start_with?: Returns `true` if 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. # * #==, #===: Returns `true` if 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?: Returns `true` if symbol is equal to a given symbol after # Unicode case folding; `false` otherwise. # # # ### 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 of `self` as 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 a Proc object which responds to the method named by # symbol. # * #to_s, #id2name: Returns the string corresponding to `self`. # * #to_sym, #intern: Returns `self`. # * #upcase: Returns symbol with all characters upcased. # class Symbol include Comparable # # Returns an array of all symbols currently in Ruby's symbol table: # # Symbol.all_symbols.size # => 9334 # Symbol.all_symbols.take(3) # => [:!, :"\"", :"#"] # def self.all_symbols: () -> Array[Symbol] # # If `object` is a symbol, returns the equivalent of `symbol.to_s <=> # object.to_s`: # # :bar <=> :foo # => -1 # :foo <=> :foo # => 0 # :foo <=> :bar # => 1 # # Otherwise, returns `nil`: # # :foo <=> 'bar' # => nil # # Related: String#<=>. # def <=>: (Symbol object) -> (-1 | 0 | 1) | (untyped) -> (-1 | 0 | 1)? # # Returns `true` if `object` is the same object as `self`, `false` otherwise. # def ==: (untyped object) -> bool # # Returns `true` if `object` is the same object as `self`, `false` otherwise. # alias === == # # Equivalent to `symbol.to_s =~ object`, including possible updates to global # variables; see String#=~. # def =~: (Regexp regex) -> Integer? | [T] (String::_MatchAgainst[String, T] object) -> T # # Equivalent to `symbol.to_s[]`; see String#[]. # def []: (int start, ?int length) -> String? | (range[int?] range) -> String? | (Regexp regexp, ?MatchData::capture backref) -> String? | (String substring) -> String? # # Equivalent to `sym.to_s.capitalize.to_sym`. # # See String#capitalize. # def capitalize: () -> Symbol | (:ascii | :lithuanian | :turkic) -> Symbol | (:lithuanian, :turkic) -> Symbol | (:turkic, :lithuanian) -> Symbol # # 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. # def casecmp: (untyped object) -> (-1 | 0 | 1)? # # 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?. # def casecmp?: (untyped object) -> bool? # # Equivalent to `sym.to_s.downcase.to_sym`. # # See String#downcase. # # Related: Symbol#upcase. # def downcase: () -> Symbol | (:ascii | :fold | :lithuanian | :turkic) -> Symbol | (:lithuanian, :turkic) -> Symbol | (:turkic, :lithuanian) -> Symbol # # Returns `true` if `self` is `:''`, `false` otherwise. # def empty?: () -> bool # # Equivalent to `self.to_s.encoding`; see String#encoding. # def encoding: () -> Encoding # # Equivalent to `self.to_s.end_with?`; see String#end_with?. # def end_with?: (*string suffixes) -> bool # # Returns a string representation of `self` (not including the leading colon): # # :foo.to_s # => "foo" # # Related: Symbol#inspect, Symbol#name. # alias id2name to_s # # Returns a string representation of `self` (including the leading colon): # # :foo.inspect # => ":foo" # # Related: Symbol#to_s, Symbol#name. # def inspect: () -> String # # alias intern to_sym # # Equivalent to `self.to_s.length`; see String#length. # def length: () -> Integer # # Equivalent to `self.to_s.match`, including possible updates to global # variables; see String#match. # def match: (Regexp | string pattern, ?int offset) -> MatchData? | [T] (Regexp | string pattern, ?int offset) { (MatchData matchdata) -> T } -> T? # # Equivalent to `sym.to_s.match?`; see String#match. # def match?: (Regexp | string pattern, ?int offset) -> bool # # Equivalent to `self.to_s.succ.to_sym`: # # :foo.succ # => :fop # # Related: String#succ. # def next: () -> Symbol # # 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. # def name: () -> String # # Equivalent to `self.to_s.length`; see String#length. # alias size length # # Equivalent to `symbol.to_s[]`; see String#[]. # alias slice [] # # Equivalent to `self.to_s.start_with?`; see String#start_with?. # def start_with?: (*Regexp | string prefixes) -> bool # # Equivalent to `self.to_s.succ.to_sym`: # # :foo.succ # => :fop # # Related: String#succ. # alias succ next # # Equivalent to `sym.to_s.swapcase.to_sym`. # # See String#swapcase. # def swapcase: () -> Symbol | (:ascii | :lithuanian | :turkic) -> Symbol | (:lithuanian, :turkic) -> Symbol | (:turkic, :lithuanian) -> Symbol # # 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.call(1000) # => "1000" # proc.call(1000, 16) # => "3e8" # (1..3).collect(&:to_s) # => ["1", "2", "3"] # def to_proc: () -> Proc # # Returns a string representation of `self` (not including the leading colon): # # :foo.to_s # => "foo" # # Related: Symbol#inspect, Symbol#name. # def to_s: () -> String # # Returns `self`. # # Related: String#to_sym. # def to_sym: () -> self # # Equivalent to `sym.to_s.upcase.to_sym`. # # See String#upcase. # def upcase: () -> Symbol | (:ascii | :lithuanian | :turkic) -> Symbol | (:lithuanian, :turkic) -> Symbol | (:turkic, :lithuanian) -> Symbol end