Class: Symbol
Overview
********************************************************************
<code>Symbol</code> objects represent names and some strings
inside the Ruby
interpreter. They are generated using the <code>:name</code> and
<code>:"string"</code> literals
syntax, and by the various <code>to_sym</code> methods. The same
<code>Symbol</code> 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 <code>Fred</code> is a constant in
one context, a method in another, and a class in a third, the
<code>Symbol</code> <code>:Fred</code> 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
Class Method Summary collapse
Instance Method Summary collapse
-
#=== ⇒ Object
Equality—At the
Object
level,==
returnstrue
only if obj and other are the same object. -
#id2name ⇒ Object
Returns the name or string corresponding to sym.
-
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
-
#to_i ⇒ Fixnum
Returns an integer that is unique for each symbol within a particular execution of a program.
-
#to_int ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
Returns the name or string corresponding to sym.
-
#to_sym ⇒ Object
In general,
to_sym
returns theSymbol
corresponding to an object.
Class Method Details
.all_symbols ⇒ Object
Instance Method Details
#==(other) ⇒ Boolean #equal?(other) ⇒ Boolean #eql?(other) ⇒ Boolean
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.
Unlike ==
, the equal?
method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b)
iff a
is the same object as b
).
The eql?
method returns true
if
<i>obj</i> and <i>anObject</i> have the
same value. Used by Hash
to test members for equality. For objects of class Object
, eql?
is synonymous with ==
. Subclasses normally continue this tradition, but there are exceptions. Numeric
types, for example, perform type conversion across ==
, but not across eql?
, so:
1 == 1.0 #=> true
1.eql? 1.0 #=> false
93 94 95 |
# File 'object.c', line 93 static VALUE rb_obj_equal(obj1, obj2) VALUE obj1, obj2; |
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
1183 1184 1185 |
# File 'object.c', line 1183 static VALUE sym_to_s(sym) VALUE sym; |
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
:fred.inspect #=> ":fred"
1152 1153 1154 |
# File 'object.c', line 1152 static VALUE sym_inspect(sym) VALUE sym; |
#to_i ⇒ Fixnum
Returns an integer that is unique for each symbol within a particular execution of a program.
:fred.to_i #=> 9809
"fred".to_sym.to_i #=> 9809
1122 1123 1124 |
# File 'object.c', line 1122 static VALUE sym_to_i(sym) VALUE sym; |
#to_int ⇒ Object
:nodoc:
1134 1135 1136 |
# File 'object.c', line 1134 static VALUE sym_to_int(sym) VALUE sym; |
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
1183 1184 1185 |
# File 'object.c', line 1183 static VALUE sym_to_s(sym) VALUE sym; |
#to_sym ⇒ Object
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
1200 1201 1202 |
# File 'object.c', line 1200 static VALUE sym_to_sym(sym) VALUE sym; |