Class: Symbol
Overview
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
Class Method Summary collapse
-
.all_symbols ⇒ Array
Returns an array of all the symbols currently in Ruby's symbol table.
Instance Method Summary collapse
-
#<=>(other) ⇒ -1, ...
Compares sym with other in string form.
-
#==(obj) ⇒ Boolean
Equality---If sym and obj are exactly the same symbol, returns
true
. -
#==(obj) ⇒ Boolean
Equality---If sym and obj are exactly the same symbol, returns
true
. -
#=~(obj) ⇒ Fixnum?
Returns
sym.to_s =~ obj
. -
#[] ⇒ Object
Returns
sym.to_s[]
. -
#capitalize ⇒ Object
Same as
sym.to_s.capitalize.intern
. -
#casecmp(other) ⇒ -1, ...
Case-insensitive version of
Symbol#<=>
. -
#downcase ⇒ Object
Same as
sym.to_s.downcase.intern
. -
#empty? ⇒ Boolean
Returns that sym is :"" or not.
-
#encoding ⇒ Encoding
Returns the Encoding object that represents the encoding of sym.
-
#id2name ⇒ Object
Returns the name or string corresponding to sym.
-
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
-
#intern ⇒ Object
In general,
to_sym
returns theSymbol
corresponding to an object. -
#length ⇒ Integer
Same as
sym.to_s.length
. -
#=~(obj) ⇒ Fixnum?
Returns
sym.to_s =~ obj
. -
#succ ⇒ Object
Same as
sym.to_s.succ.intern
. -
#length ⇒ Integer
Same as
sym.to_s.length
. -
#slice ⇒ Object
Returns
sym.to_s[]
. -
#succ ⇒ Object
Same as
sym.to_s.succ.intern
. -
#swapcase ⇒ Object
Same as
sym.to_s.swapcase.intern
. -
#to_proc ⇒ Object
Returns a Proc object which respond to the given method by sym.
-
#to_s ⇒ Object
Returns the name or string corresponding to sym.
-
#to_sym ⇒ Object
In general,
to_sym
returns theSymbol
corresponding to an object. -
#upcase ⇒ Object
Same as
sym.to_s.upcase.intern
.
Methods included from Comparable
Class Method Details
.all_symbols ⇒ Array
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, :$>]
|
# File 'string.c'
VALUE
rb_sym_all_symbols(void)
{
VALUE ary = rb_ary_new2(global_symbols.sym_id->num_entries);
st_foreach(global_symbols.sym_id, symbols_i, ary);
return ary;
}
|
Instance Method Details
#<=>(other) ⇒ -1, ...
Compares sym with other in string form.
|
# File 'string.c'
static VALUE
sym_cmp(VALUE sym, VALUE other)
{
if (!SYMBOL_P(other)) {
return Qnil;
}
|
#==(obj) ⇒ Boolean
Equality---If sym and obj are exactly the same symbol, returns true
.
|
# File 'string.c'
static VALUE
sym_equal(VALUE sym1, VALUE sym2)
{
if (sym1 == sym2) return Qtrue;
return Qfalse;
}
|
#==(obj) ⇒ Boolean
Equality---If sym and obj are exactly the same symbol, returns true
.
|
# File 'string.c'
static VALUE
sym_equal(VALUE sym1, VALUE sym2)
{
if (sym1 == sym2) return Qtrue;
return Qfalse;
}
|
#=~(obj) ⇒ Fixnum?
Returns sym.to_s =~ obj
.
|
# File 'string.c'
static VALUE
sym_match(VALUE sym, VALUE other)
{
return rb_str_match(rb_sym_to_s(sym), other);
}
|
#[](idx) ⇒ String #[](b, n) ⇒ String
Returns sym.to_s[]
.
|
# File 'string.c'
static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
return rb_str_aref_m(argc, argv, rb_sym_to_s(sym));
}
|
#capitalize ⇒ Object
Same as sym.to_s.capitalize.intern
.
|
# File 'string.c'
static VALUE
sym_capitalize(VALUE sym)
{
return rb_str_intern(rb_str_capitalize(rb_id2str(SYM2ID(sym))));
}
|
#casecmp(other) ⇒ -1, ...
Case-insensitive version of Symbol#<=>
.
|
# File 'string.c'
static VALUE
sym_casecmp(VALUE sym, VALUE other)
{
if (!SYMBOL_P(other)) {
return Qnil;
}
|
#downcase ⇒ Object
Same as sym.to_s.downcase.intern
.
|
# File 'string.c'
static VALUE
sym_downcase(VALUE sym)
{
return rb_str_intern(rb_str_downcase(rb_id2str(SYM2ID(sym))));
}
|
#empty? ⇒ Boolean
Returns that sym is :"" or not.
|
# File 'string.c'
static VALUE
sym_empty(VALUE sym)
{
return rb_str_empty(rb_id2str(SYM2ID(sym)));
}
|
#encoding ⇒ Encoding
Returns the Encoding object that represents the encoding of sym.
|
# File 'string.c'
static VALUE
sym_encoding(VALUE sym)
{
return rb_obj_encoding(rb_id2str(SYM2ID(sym)));
}
|
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
|
# File 'string.c'
VALUE
rb_sym_to_s(VALUE sym)
{
ID id = SYM2ID(sym);
return str_new3(rb_cString, rb_id2str(id));
}
|
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
:fred.inspect #=> ":fred"
|
# File 'string.c'
static VALUE
sym_inspect(VALUE sym)
{
VALUE str;
ID id = SYM2ID(sym);
rb_encoding *enc;
const char *ptr;
long len;
char *dest;
rb_encoding *resenc = rb_default_internal_encoding();
if (resenc == NULL) resenc = rb_default_external_encoding();
sym = rb_id2str(id);
enc = STR_ENC_GET(sym);
ptr = RSTRING_PTR(sym);
len = RSTRING_LEN(sym);
if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) ||
!rb_enc_symname_p(ptr, enc) || !sym_printable(ptr, ptr + len, enc)) {
str = rb_str_inspect(sym);
len = RSTRING_LEN(str);
rb_str_resize(str, len + 1);
dest = RSTRING_PTR(str);
memmove(dest + 1, dest, len);
dest[0] = ':';
}
|
#to_sym ⇒ Object #intern ⇒ Object
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
|
# File 'string.c'
static VALUE
sym_to_sym(VALUE sym)
{
return sym;
}
|
#length ⇒ Integer
Same as sym.to_s.length
.
|
# File 'string.c'
static VALUE
sym_length(VALUE sym)
{
return rb_str_length(rb_id2str(SYM2ID(sym)));
}
|
#=~(obj) ⇒ Fixnum?
Returns sym.to_s =~ obj
.
|
# File 'string.c'
static VALUE
sym_match(VALUE sym, VALUE other)
{
return rb_str_match(rb_sym_to_s(sym), other);
}
|
#succ ⇒ Object
Same as sym.to_s.succ.intern
.
|
# File 'string.c'
static VALUE
sym_succ(VALUE sym)
{
return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}
|
#length ⇒ Integer
Same as sym.to_s.length
.
|
# File 'string.c'
static VALUE
sym_length(VALUE sym)
{
return rb_str_length(rb_id2str(SYM2ID(sym)));
}
|
#[](idx) ⇒ String #[](b, n) ⇒ String
Returns sym.to_s[]
.
|
# File 'string.c'
static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
return rb_str_aref_m(argc, argv, rb_sym_to_s(sym));
}
|
#succ ⇒ Object
Same as sym.to_s.succ.intern
.
|
# File 'string.c'
static VALUE
sym_succ(VALUE sym)
{
return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}
|
#swapcase ⇒ Object
Same as sym.to_s.swapcase.intern
.
|
# File 'string.c'
static VALUE
sym_swapcase(VALUE sym)
{
return rb_str_intern(rb_str_swapcase(rb_id2str(SYM2ID(sym))));
}
|
#to_proc ⇒ Object
Returns a Proc object which respond to the given method by sym.
(1..3).collect(&:to_s) #=> ["1", "2", "3"]
|
# File 'string.c'
static VALUE
sym_to_proc(VALUE sym)
{
static VALUE sym_proc_cache = Qfalse;
enum {SYM_PROC_CACHE_SIZE = 67}
|
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
|
# File 'string.c'
VALUE
rb_sym_to_s(VALUE sym)
{
ID id = SYM2ID(sym);
return str_new3(rb_cString, rb_id2str(id));
}
|
#to_sym ⇒ Object #intern ⇒ Object
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
|
# File 'string.c'
static VALUE
sym_to_sym(VALUE sym)
{
return sym;
}
|
#upcase ⇒ Object
Same as sym.to_s.upcase.intern
.
|
# File 'string.c'
static VALUE
sym_upcase(VALUE sym)
{
return rb_str_intern(rb_str_upcase(rb_id2str(SYM2ID(sym))));
}
|