Class Symbol
In: string.c
lib/rexml/xpath_parser.rb
lib/yaml/rubytypes.rb
Parent: Object

Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods. 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

Methods

==   all_symbols   dclone   id2name   inspect   intern   to_proc   to_s   to_sym   to_yaml   yaml_new  

Included Modules

Comparable

Public Class methods

Returns an array of all the symbols currently in Ruby‘s symbol table.

   Symbol.all_symbols.size    #=> 903
   Symbol.all_symbols[1,20]   #=> [:floor, :ARGV, :Binding, :symlink,
                                   :chown, :EOFError, :$;, :String,
                                   :LOCK_SH, :"setuid?", :$<,
                                   :default_proc, :compact, :extend,
                                   :Tms, :getwd, :$=, :ThreadGroup,
                                   :wait2, :$>]

Public Instance methods

Equality—If sym and obj are exactly the same symbol, returns true. Otherwise, compares them as strings.

Returns the name or string corresponding to sym.

   :fred.id2name   #=> "fred"

Returns the representation of sym as a symbol literal.

   :fred.inspect   #=> ":fred"

In general, to_sym returns the Symbol corresponding to an object. As sym is already a symbol, self is returned in this case.

Returns a Proc object which respond to the given method by sym.

  (1..3).collect(&:to_s)  #=> ["1", "2", "3"]

Returns the name or string corresponding to sym.

   :fred.id2name   #=> "fred"

In general, to_sym returns the Symbol corresponding to an object. As sym is already a symbol, self is returned in this case.

[Validate]

ruby-doc.org is hosted and run by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. Ruby-doc.org was created in 2002 to promote the Ruby language and to help other Ruby hackers.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.