Ruby | Symbol Class Last Updated : 11 Jul, 2025 Suggest changes Share Like Article Like Report 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: Ruby # Ruby program to illustrate # Symbol objects # context 3 module Geeks1 class Max end $a1 = :Max end # context 1 module Geeks2 Max = 1 $a2 = :Max end # context 2 def Max() end $a3 = :Max puts $a1.object_id puts $a2.object_id puts $a3.object_id 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: Ruby # Ruby program to illustrate # the use of all_symbol method # Using all_symbol method puts Symbol.all_symbols.size puts Symbol.all_symbols[1, 20] Output: 3250 " # $ % & ' ( ) * + , - . / : ; < = > ? Instance Methods id2name : This method returns a string that is representation of sym. sym.id2name Example: Ruby # Ruby program to illustrate # the use of id2name method # Using id2name method p :Geeks.id2name p :"Welcome to GeeksforGeeks Portal".id2name Output: "Geeks" "Welcome to GeeksforGeeks Portal" inspect : This method return the representation of sym in the form of symbol literal. sym.inspect Example: Ruby # Ruby program to illustrate # the use of inspect method # Using inspect method p :geeks.inspect p :"welcome to geeksforgeeks portal".inspect Output: ":geeks" ":\"welcome to geeksforgeeks portal\"" 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: Ruby # Ruby program to illustrate # the use of to_s method # Using to_s method p :geeks.to_s p :"welcome to geeksforgeeks portal".to_s Output: "geeks" "welcome to geeksforgeeks portal" <=> :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: Ruby # Ruby program to illustrate # use of <=> # Using <=> a= :geeks b = :"welcome to geeksforgeeks portal" puts a<=>b c= :geeks puts a<=>c puts b<=>a Output: -1 0 1 == : It returns true if the sym is equal to obj, otherwise it return false. sym== obj Example: Ruby # Ruby program to illustrate # use of == # Using == a= :geeks b = :"welcome to geeksforgeeks portal" puts a==b c= :geeks puts a==c Output: false true [] : This method returns the value of sym.to_s[]. sym[idx] --> char sym[b, n] --> string capitalize : This method is similar to Symbol#to_s. sym.capitalize 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: Ruby # Ruby program to illustrate # use of casecmp method # Using casecmp method puts :GeeKs.casecmp(:geeks) puts :GeeKsGfg.casecmp(:geeksG) puts :GeeKsGfg.casecmp(:geeksGfgz) puts :GeeKsGfg.casecmp(3) Output: 0 1 -1 nil downcase : This method converts upper-case letters in lower-case. sym.downcase Example: Ruby # Ruby program to illustrate # use of the downcase method # Using the downcase method puts :"WELCOME TO GEEKSFORGEEKS".downcase Output: :"welcome to geeksforgeeks" length : This method returns the length of the given sym. sym.length Example: Ruby # Ruby program to illustrate # use of length method # Using length method puts :GeeKsGfg.length Output: 8 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: Ruby # Ruby program to illustrate # use of slice method # Using slice method p :GeeKsGfg.slice(3) p :GeeKsGfg.slice(6) Output: "K" "f" 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: Ruby # Ruby program to illustrate # use of the swapcase method # Using swapcase method p "WELcome TO geeksFORGEEKS".swapcase Output: "welCOME to GEEKSforgeeks" upcase : This method converts lower-case characters into upper-case. sym.upcase Example: Ruby # Ruby program to illustrate # use of the upcase method # Using upcase method p "welcome to geeksforgeeks".upcase Output: "WELCOME TO GEEKSFORGEEKS" to_proc : This method return a Proc object which answer to the given method by sym. sym.to_proc Example: Ruby # Ruby program to illustrate # use of to_proc method # Using to_proc method p (1..5).collect(&:to_s) Output: ["1", "2", "3", "4", "5"] 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 A ankita_saini Follow Article Tags : Misc Ruby Ruby-Built-in-class Explore Ruby Programming Language 4 min read OverviewRuby For Beginners 3 min read Ruby Programming Language (Introduction) 4 min read Comparison of Java with Other Programming Languages 4 min read Similarities and Differences between Ruby and C language 3 min read Similarities and Differences between Ruby and C++ 3 min read Environment Setup in Ruby 3 min read How to install Ruby on Linux? 2 min read How to install Ruby on Windows? 2 min read Interesting facts about Ruby Programming Language 2 min read BasicsRuby | Keywords 4 min read Ruby | Data Types 3 min read Ruby Basic Syntax 3 min read Hello World in Ruby 2 min read Ruby | Types of Variables 4 min read Global Variable in Ruby 2 min read Comments in Ruby 2 min read Ruby | Ranges 4 min read Ruby Literals 4 min read Ruby Directories 5 min read Ruby | Operators 11 min read Operator Precedence in Ruby 2 min read Operator Overloading in Ruby 5 min read Ruby | Pre-define Variables & Constants 5 min read Ruby | unless Statement and unless Modifier 2 min read Control StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1 3 min read Ruby | Loops (for, while, do..while, until) 5 min read Ruby | Case Statement 3 min read Ruby | Control Flow Alteration 7 min read Ruby Break and Next Statement 2 min read Ruby redo and retry Statement 2 min read BEGIN and END Blocks In Ruby 2 min read File Handling in Ruby 4 min read MethodsRuby | Methods 3 min read Method Visibility in Ruby 3 min read Recursion in Ruby 4 min read Ruby Hook Methods 5 min read Ruby | Range Class Methods 5 min read The Initialize Method in Ruby 2 min read Ruby | Method overriding 2 min read Ruby Date and Time 3 min read OOP ConceptsObject-Oriented Programming in Ruby | Set 1 9 min read Object Oriented Programming in Ruby | Set-2 8 min read Ruby | Class & Object 4 min read Private Classes in Ruby 3 min read Freezing Objects | Ruby 2 min read Ruby | Inheritance 4 min read Polymorphism in Ruby 3 min read Ruby | Constructors 2 min read Ruby | Access Control 8 min read Ruby | Encapsulation 2 min read Ruby Mixins 3 min read Instance Variables in Ruby 3 min read Data Abstraction in Ruby 3 min read Ruby Static Members 3 min read ExceptionsRuby | Exceptions 4 min read Ruby | Exception handling 6 min read Catch and Throw Exception In Ruby 3 min read Raising Exceptions in Ruby 4 min read Ruby | Exception Handling in Threads | Set - 1 2 min read Ruby | Exception Class and its Methods 3 min read Ruby RegexRuby | Regular Expressions 3 min read Ruby Search and Replace 2 min read Ruby ClassesRuby | Float Class 7 min read Ruby | Integer Class 3 min read Ruby | Symbol Class 5 min read Ruby | Struct Class 5 min read Ruby | Dir Class and its methods 3 min read Ruby | MatchData Class 4 min read Ruby ModuleRuby | Module 4 min read Ruby | Comparable Module 3 min read Ruby | Math Module 4 min read Include v/s Extend in Ruby 2 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like