VOOZH about

URL: https://www.geeksforgeeks.org/ruby/ruby-symbol-class/

⇱ Ruby | Symbol Class - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ruby | Symbol Class

Last Updated : 11 Jul, 2025
The objects of the Symbol class represent the names present inside the Ruby interpreter. They are usually generated by using :name literal syntax or by using to_sym methods. The similar Symbol objects are created for a given name string for the duration of a program's execution, regardless of the content and meaning of the name. Example: Output:
1675428
1675428
1675428
Explanation: If Max is a constant in context1, a method in context2, or class in the context3, then this :Max will be the same object in all given contexts.

Class Method

all_symbols : This method returns an array of symbols that currently present in the Ruby's symbol table.
Symbol.all_symbols
Example: Output:
3250
"
#
$
%
&
'
(
)
*
+
,
-
.
/
:
;
<
=
>
?

Instance Methods

  1. id2name : This method returns a string that is representation of sym.
    sym.id2name
    Example: Output:
    "Geeks"
    "Welcome to GeeksforGeeks Portal"
    
  2. inspect : This method return the representation of sym in the form of symbol literal.
    sym.inspect
    Example: Output:
    ":geeks"
    ":\"welcome to geeksforgeeks portal\""
    
  3. to_s : This method is similar to Symbol#id2name. This method returns the name or a string that corresponding to sym.
    sym.to_s
    Example: Output:
    "geeks"
    "welcome to geeksforgeeks portal"
    
  4. <=> :It compares sym to other_sym after calling to_s. It returns -1 if sym is less than other_sym, it returns 0 if sym is equal to other_sym, or it returns +1 if sym is greater than other_sym.
    sym <=> other_sym 
    Example: Output:
    -1
    0
    1
    
  5. == : It returns true if the sym is equal to obj, otherwise it return false.
    sym== obj
    Example: Output:
    false
    true
    
  6. [] : This method returns the value of sym.to_s[].
    sym[idx] --> char 
    sym[b, n] --> string
    
  7. capitalize : This method is similar to Symbol#to_s.
    sym.capitalize
  8. casecmp : This method is case-insensitive version of symbol <=$gt;. It will return -1, 0, 1, or nil. It is worked on A-Z/a-z, not on all Unicode. In this method nil is returned when the two symbols have incompatible encodings or if other_sym is not a symbol.
    sym.casecmp(other)
    Example: Output:
    0
    1
    -1
    nil
  9. downcase : This method converts upper-case letters in lower-case.
    sym.downcase
    Example: Output:
    :"welcome to geeksforgeeks"
  10. length : This method returns the length of the given sym.
    sym.length
    Example: Output:
    8
  11. slice : This method is similar to Symbol#to_s. This method provides you character on the given index from the sym .
    sym.slice(index)
    sym.slice(b, n)
    
    Example: Output:
    "K"
    "f"
    
  12. swapcase : This method interchange the case of the characters that present in sym. In other words, it converts lower-case into upper-case and upper-case into lower-case.
    sym.swapcase
    Example: Output:
    "welCOME to GEEKSforgeeks"
  13. upcase : This method converts lower-case characters into upper-case.
    sym.upcase
    Example: Output:
    "WELCOME TO GEEKSFORGEEKS"
  14. to_proc : This method return a Proc object which answer to the given method by sym.
    sym.to_proc
    Example: Output:
     ["1", "2", "3", "4", "5"]
  15. to_sym This method returns a symbol that corresponding to an object. Here sym has been already a symbol, so in this case it returns it.
    sym.to_sym
Reference: https://ruby-doc.org/core-2.5.0/Symbol.html#method-i-5B-5D
Comment
Article Tags: