Class: Symbol
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
::Comparable
|
|
Inherits: | Object |
Defined in: | string.c, string.c, symbol.c |
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
-
.all_symbols ⇒ Array
Returns an array of all the symbols currently in Ruby's symbol table.
Instance Attribute Summary
-
#empty? ⇒ Boolean
readonly
Returns whether sym is :“” or not.
Instance Method Summary
-
#<=>(other_symbol) ⇒ 1, ...
Compares
symbol
withother_symbol
after calling #to_s on each of the symbols. - #==
- #===
-
#=~(obj) ⇒ Integer?
Returns
sym.to_s =~ obj
. -
#[](idx) ⇒ String
(also: #slice)
Returns
sym.to_s[]
. -
#capitalize ⇒ Symbol
Same as
sym.to_s.capitalize.intern
. -
#casecmp(other_symbol) ⇒ 1, ...
Case-insensitive version of #<=>.
-
#casecmp?(other_symbol) ⇒ true, ...
Returns
true
ifsym
andother_symbol
are equal after Unicode case folding,false
if they are not equal. -
#downcase ⇒ Symbol
Same as
sym.to_s.downcase.intern
. -
#encoding ⇒ Encoding
Returns the ::Encoding object that represents the encoding of sym.
-
#id2name ⇒ String
Alias for #to_s.
-
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
-
#intern ⇒ sym
(also: #to_sym)
In general, #to_sym returns the
Symbol
corresponding to an object. -
#length ⇒ Integer
(also: #size)
Same as
sym.to_s.length
. -
#match(pattern) ⇒ MatchData?
Returns
sym.to_s.match
. -
#match?(pattern) ⇒ Boolean
Returns
sym.to_s.match?
. -
#next
(also: #succ)
Same as
sym.to_s.succ.intern
. -
#size ⇒ Integer
Alias for #length.
-
#slice(idx) ⇒ String
Alias for #[].
-
#succ
Alias for #next.
-
#swapcase ⇒ Symbol
Same as
sym.to_s.swapcase.intern
. -
#to_proc ⇒ Proc
Returns a Proc object which respond to the given method by sym.
-
#to_s ⇒ String
(also: #id2name)
Returns the name or string corresponding to sym.
-
#to_sym ⇒ sym
Alias for #intern.
-
#upcase ⇒ Symbol
Same as
sym.to_s.upcase.intern
.
::Comparable - Included
#< | Compares two objects based on the receiver's #<=> method, returning true if it returns -1. |
#<= | Compares two objects based on the receiver's #<=> method, returning true if it returns -1 or 0. |
#== | Compares two objects based on the receiver's #<=> method, returning true if it returns 0. |
#> | Compares two objects based on the receiver's #<=> method, returning true if it returns 1. |
#>= | Compares two objects based on the receiver's #<=> method, returning true if it returns 0 or 1. |
#between? | |
#clamp |
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 'symbol.c', line 829
VALUE rb_sym_all_symbols(void) { VALUE ary = rb_ary_new2(global_symbols.str_sym->num_entries); st_foreach(global_symbols.str_sym, symbols_i, ary); return ary; }
Instance Attribute Details
#empty? ⇒ Boolean
(readonly)
Returns whether sym is :“” or not.
# File 'string.c', line 10725
static VALUE sym_empty(VALUE sym) { return rb_str_empty(rb_sym2str(sym)); }
Instance Method Details
#<=>(other_symbol) ⇒ 1
, ...
Compares symbol
with other_symbol
after calling #to_s on each of the symbols. Returns -1, 0, +1, or nil
depending on whether symbol
is less than, equal to, or greater than other_symbol
.
nil
is returned if the two values are incomparable.
See String#<=> for more information.
# File 'string.c', line 10580
static VALUE sym_cmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return rb_str_cmp_m(rb_sym2str(sym), rb_sym2str(other)); }
#==
[ GitHub ]#===
[ GitHub ]#=~(obj) ⇒ Integer?
Returns sym.to_s =~ obj
.
# File 'string.c', line 10654
static VALUE sym_match(VALUE sym, VALUE other) { return rb_str_match(rb_sym2str(sym), other); }
Also known as: #slice
Returns sym.to_s[]
.
# File 'string.c', line 10698
static VALUE sym_aref(int argc, VALUE *argv, VALUE sym) { return rb_str_aref_m(argc, argv, rb_sym2str(sym)); }
#capitalize ⇒ Symbol
#capitalize([options]) ⇒ Symbol
Symbol
#capitalize([options]) ⇒ Symbol
Same as sym.to_s.capitalize.intern
.
# File 'string.c', line 10767
static VALUE sym_capitalize(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_capitalize(argc, argv, rb_sym2str(sym))); }
#casecmp(other_symbol) ⇒ 1
, ...
Case-insensitive version of #<=>. Currently, case-insensitivity only works on characters A-Z/a-z, not all of Unicode. This is different from #casecmp?.
:aBcDeF.casecmp(:abcde) #=> 1
:aBcDeF.casecmp(:abcdef) #=> 0
:aBcDeF.casecmp(:abcdefg) #=> -1
:abcdef.casecmp(:ABCDEF) #=> 0
nil
is returned if the two symbols have incompatible encodings, or if other_symbol
is not a symbol.
:foo.casecmp(2) #=> nil
"\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp(:"\u{c4 d6 dc}") #=> nil
# File 'string.c', line 10609
static VALUE sym_casecmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return str_casecmp(rb_sym2str(sym), rb_sym2str(other)); }
#casecmp?(other_symbol) ⇒ true
, ...
Returns true
if sym
and other_symbol
are equal after Unicode case folding, false
if they are not equal.
:aBcDeF.casecmp?(:abcde) #=> false
:aBcDeF.casecmp?(:abcdef) #=> true
:aBcDeF.casecmp?(:abcdefg) #=> false
:abcdef.casecmp?(:ABCDEF) #=> true
:"\u{e4 f6 fc}".casecmp?(:"\u{c4 d6 dc}") #=> true
nil
is returned if the two symbols have incompatible encodings, or if other_symbol
is not a symbol.
:foo.casecmp?(2) #=> nil
"\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp?(:"\u{c4 d6 dc}") #=> nil
# File 'string.c', line 10638
static VALUE sym_casecmp_p(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return str_casecmp_p(rb_sym2str(sym), rb_sym2str(other)); }
#downcase ⇒ Symbol
#downcase([options]) ⇒ Symbol
Symbol
#downcase([options]) ⇒ Symbol
Same as sym.to_s.downcase.intern
.
# File 'string.c', line 10753
static VALUE sym_downcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_downcase(argc, argv, rb_sym2str(sym))); }
#encoding ⇒ Encoding
Returns the ::Encoding object that represents the encoding of sym.
# File 'string.c', line 10794
static VALUE sym_encoding(VALUE sym) { return rb_obj_encoding(rb_sym2str(sym)); }
Alias for #to_s.
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
:fred.inspect #=> ":fred"
# File 'string.c', line 10462
static VALUE sym_inspect(VALUE sym) { VALUE str = rb_sym2str(sym); const char *ptr; long len; char *dest; if (!rb_str_symname_p(str)) { str = rb_str_inspect(str); len = RSTRING_LEN(str); rb_str_resize(str, len + 1); dest = RSTRING_PTR(str); memmove(dest + 1, dest, len); } else { rb_encoding *enc = STR_ENC_GET(str); RSTRING_GETMEM(str, ptr, len); str = rb_enc_str_new(0, len + 1, enc); dest = RSTRING_PTR(str); memcpy(dest + 1, ptr, len); } dest[0] = ':'; return str; }
#to_sym ⇒ sym
#intern ⇒ sym
Also known as: #to_sym
sym
#intern ⇒ sym
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', line 10518
static VALUE sym_to_sym(VALUE sym) { return sym; }
Also known as: #size
Same as sym.to_s.length
.
# File 'string.c', line 10712
static VALUE sym_length(VALUE sym) { return rb_str_length(rb_sym2str(sym)); }
Returns sym.to_s.match
.
# File 'string.c', line 10668
static VALUE sym_match_m(int argc, VALUE *argv, VALUE sym) { return rb_str_match_m(argc, argv, rb_sym2str(sym)); }
#match?(pattern) ⇒ Boolean
#match?(pattern, pos) ⇒ Boolean
Boolean
#match?(pattern, pos) ⇒ Boolean
Returns sym.to_s.match?
.
# File 'string.c', line 10682
static VALUE sym_match_m_p(int argc, VALUE *argv, VALUE sym) { return rb_str_match_m_p(argc, argv, sym); }
#next Also known as: #succ
Same as sym.to_s.succ.intern
.
# File 'string.c', line 10560
static VALUE sym_succ(VALUE sym) { return rb_str_intern(rb_str_succ(rb_sym2str(sym))); }
Alias for #length.
Alias for #[].
#next
#succ
Alias for #next.
#swapcase ⇒ Symbol
#swapcase([options]) ⇒ Symbol
Symbol
#swapcase([options]) ⇒ Symbol
Same as sym.to_s.swapcase.intern
.
# File 'string.c', line 10781
static VALUE sym_swapcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym))); }
#to_proc ⇒ Proc
Returns a Proc object which respond to the given method by sym.
(1..3).collect(&:to_s) #=> ["1", "2", "3"]
# File 'string.c', line 10546
VALUE rb_sym_to_proc(VALUE sym) { }
Also known as: #id2name
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
:ginger.to_s #=> "ginger"
# File 'string.c', line 10501
VALUE rb_sym_to_s(VALUE sym) { return str_new_shared(rb_cString, rb_sym2str(sym)); }
#to_sym ⇒ sym
#intern ⇒ sym
sym
#intern ⇒ sym
Alias for #intern.
#upcase ⇒ Symbol
#upcase([options]) ⇒ Symbol
Symbol
#upcase([options]) ⇒ Symbol
Same as sym.to_s.upcase.intern
.
# File 'string.c', line 10739
static VALUE sym_upcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_upcase(argc, argv, rb_sym2str(sym))); }