Ruby | Symbol Class - GeeksforGeeks

11 Oct, 2019
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 program to illustrate # Symbol objects  # context 3module Geeks1  class Maxend$a1 = :Maxend  # context 1module Geeks2  Max = 1$a2 = :Maxend  # context 2def 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.
all_symbols : This method returns an array of symbols that currently present in the Ruby’s symbol table.
Symbol.all_symbols
Example:
# Ruby program to illustrate # the use of all_symbol method  # Using all_symbol methodputs Symbol.all_symbols.size puts Symbol.all_symbols[1, 20]
Output:
3250
"
#
$
%
&
'
(
)
*
+
,
-
.
/
:
;
<
=
>
?

id2name : This method returns a string that is representation of sym.sym.id2nameExample:# Ruby program to illustrate # the use of id2name method  # Using id2name methodp :Geeks.id2name p :"Welcome to GeeksforGeeks Portal".id2nameOutput:"Geeks"
"Welcome to GeeksforGeeks Portal"
inspect : This method return the representation of sym in the form of symbol literal.sym.inspectExample:# Ruby program to illustrate # the use of inspect method  # Using inspect methodp :geeks.inspectp :"welcome to geeksforgeeks portal".inspectOutput:":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_sExample:# Ruby program to illustrate # the use of to_s method  # Using to_s methodp :geeks.to_sp :"welcome to geeksforgeeks portal".to_sOutput:"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 program to illustrate # use of <=>  # Using <=>a= :geeksb = :"welcome to geeksforgeeks portal"puts a<=>bc= :geeksputs a<=>cputs b<=>aOutput:-1
0
1
== : It returns true if the sym is equal to obj, otherwise it return false.sym== objExample:# Ruby program to illustrate # use of ==  # Using ==a= :geeksb = :"welcome to geeksforgeeks portal"puts a==bc= :geeksputs a==cOutput: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.capitalizecasecmp : 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 program to illustrate # use of casecmp method  # Using casecmp methodputs :GeeKs.casecmp(:geeks)puts :GeeKsGfg.casecmp(:geeksG)puts :GeeKsGfg.casecmp(:geeksGfgz)puts :GeeKsGfg.casecmp(3)Output:0
1
-1
nildowncase : This method converts upper-case letters in lower-case.sym.downcaseExample:# Ruby program to illustrate # use of the downcase method  # Using the downcase methodputs :"WELCOME TO GEEKSFORGEEKS".downcaseOutput::"welcome to geeksforgeeks"length : This method returns the length of the given sym.sym.lengthExample:# Ruby program to illustrate # use of length method  # Using length methodputs :GeeKsGfg.lengthOutput:8slice : 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 program to illustrate # use of slice method  # Using slice methodp :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.swapcaseExample:# Ruby program to illustrate # use of the swapcase method  # Using swapcase methodp "WELcome TO geeksFORGEEKS".swapcaseOutput:"welCOME to GEEKSforgeeks"upcase : This method converts lower-case characters into upper-case.sym.upcaseExample:# Ruby program to illustrate # use of the upcase method  # Using upcase methodp "welcome to geeksforgeeks".upcaseOutput:"WELCOME TO GEEKSFORGEEKS"to_proc : This method return a Proc object which answer to the given method by sym.sym.to_procExample:# Ruby program to illustrate # use of to_proc method  # Using to_proc methodp (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
id2name : This method returns a string that is representation of sym.sym.id2nameExample:# Ruby program to illustrate # the use of id2name method  # Using id2name methodp :Geeks.id2name p :"Welcome to GeeksforGeeks Portal".id2nameOutput:"Geeks"
"Welcome to GeeksforGeeks Portal"

sym.id2name
Example:
# Ruby program to illustrate # the use of id2name method  # Using id2name methodp :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.inspectExample:# Ruby program to illustrate # the use of inspect method  # Using inspect methodp :geeks.inspectp :"welcome to geeksforgeeks portal".inspectOutput:":geeks"
":\"welcome to geeksforgeeks portal\""

sym.inspect
Example:
# Ruby program to illustrate # the use of inspect method  # Using inspect methodp :geeks.inspectp :"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_sExample:# Ruby program to illustrate # the use of to_s method  # Using to_s methodp :geeks.to_sp :"welcome to geeksforgeeks portal".to_sOutput:"geeks"
"welcome to geeksforgeeks portal"

sym.to_s
Example:
# Ruby program to illustrate # the use of to_s method  # Using to_s methodp :geeks.to_sp :"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 program to illustrate # use of <=>  # Using <=>a= :geeksb = :"welcome to geeksforgeeks portal"puts a<=>bc= :geeksputs a<=>cputs b<=>aOutput:-1
0
1

sym <=> other_sym 
Example:
# Ruby program to illustrate # use of <=>  # Using <=>a= :geeksb = :"welcome to geeksforgeeks portal"puts a<=>bc= :geeksputs a<=>cputs b<=>a
Output:
-1
0
1

== : It returns true if the sym is equal to obj, otherwise it return false.sym== objExample:# Ruby program to illustrate # use of ==  # Using ==a= :geeksb = :"welcome to geeksforgeeks portal"puts a==bc= :geeksputs a==cOutput:false
true

sym== obj
Example:
# Ruby program to illustrate # use of ==  # Using ==a= :geeksb = :"welcome to geeksforgeeks portal"puts a==bc= :geeksputs a==c
Output:
false
true

[] : This method returns the value of sym.to_s[].sym[idx] --> char 
sym[b, n] --> string

sym[idx] --> char 
sym[b, n] --> string

capitalize : This method is similar to Symbol#to_s.sym.capitalize
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 program to illustrate # use of casecmp method  # Using casecmp methodputs :GeeKs.casecmp(:geeks)puts :GeeKsGfg.casecmp(:geeksG)puts :GeeKsGfg.casecmp(:geeksGfgz)puts :GeeKsGfg.casecmp(3)Output:0
1
-1
nil
sym.casecmp(other)
Example:
# Ruby program to illustrate # use of casecmp method  # Using casecmp methodputs :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.downcaseExample:# Ruby program to illustrate # use of the downcase method  # Using the downcase methodputs :"WELCOME TO GEEKSFORGEEKS".downcaseOutput::"welcome to geeksforgeeks"
sym.downcase
Example:
# Ruby program to illustrate # use of the downcase method  # Using the downcase methodputs :"WELCOME TO GEEKSFORGEEKS".downcase
Output:
:"welcome to geeksforgeeks"
length : This method returns the length of the given sym.sym.lengthExample:# Ruby program to illustrate # use of length method  # Using length methodputs :GeeKsGfg.lengthOutput:8
sym.length
Example:
# Ruby program to illustrate # use of length method  # Using length methodputs :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 program to illustrate # use of slice method  # Using slice methodp :GeeKsGfg.slice(3)p :GeeKsGfg.slice(6)Output:"K"
"f"

sym.slice(index)
sym.slice(b, n)

Example:
# Ruby program to illustrate # use of slice method  # Using slice methodp :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.swapcaseExample:# Ruby program to illustrate # use of the swapcase method  # Using swapcase methodp "WELcome TO geeksFORGEEKS".swapcaseOutput:"welCOME to GEEKSforgeeks"
sym.swapcase
Example:
# Ruby program to illustrate # use of the swapcase method  # Using swapcase methodp "WELcome TO geeksFORGEEKS".swapcase
Output:
"welCOME to GEEKSforgeeks"
upcase : This method converts lower-case characters into upper-case.sym.upcaseExample:# Ruby program to illustrate # use of the upcase method  # Using upcase methodp "welcome to geeksforgeeks".upcaseOutput:"WELCOME TO GEEKSFORGEEKS"
sym.upcase
Example:
# Ruby program to illustrate # use of the upcase method  # Using upcase methodp "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_procExample:# Ruby program to illustrate # use of to_proc method  # Using to_proc methodp (1..5).collect(&:to_s)Output: ["1", "2", "3", "4", "5"]
sym.to_proc
Example:
# Ruby program to illustrate # use of to_proc method  # Using to_proc methodp (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
sym.to_sym
Reference: https://ruby-doc.org/core-2.5.0/Symbol.html#method-i-5B-5D
nidhi_biet
Ruby-Built-in-class
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Include v/s Extend in Ruby
Global Variable in Ruby
Ruby | Hash delete() function
Ruby | Enumerator each_with_index function
Ruby | Types of Variables
Ruby | Case Statement
Ruby | Array select() function
Ruby | Data Types
Ruby | Numeric round() function
Ruby | String capitalize() Method