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.id #=> 2514190 $f2.id #=> 2514190 $f3.id #=> 2514190
| Public Methods | |
|---|---|
| === | Equality—At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning. |
| all_ |
Returns an array of all the symbols currently in Ruby’s symbol table. |
| id2name | Returns the name or string corresponding to sym. |
| inspect | Returns the representation of sym as a symbol literal. |
| to_ |
Returns an integer that is unique for each symbol within a particular execution of a program. |
| to_ |
:nodoc: |
| to_ |
Returns the name or string corresponding to sym. |
| to_ |
In general, to_sym returns the Symbol corresponding to an object. As sym is already a symbol, self is returned in this case. |
<code/>and<pre/>for code samples.