Class: Symbol
Overview
********************************************************************
\Symbol objects represent named identifiers inside the Ruby interpreter.
You can create a \Symbol object explicitly with:
- A {symbol literal}[rdoc-ref:syntax/literals.rdoc@Symbol+Literals].
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 <code>Fred</code> is a constant in one context, a method in
another, and a class in a third, the \Symbol <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.object_id #=> 2514190
$f2.object_id #=> 2514190
$f3.object_id #=> 2514190
Constant, method, and variable names are returned as symbols:
module One
Two = 2
def three; 3 end
@four = 4
@@five = 5
$six = 6
end
seven = 7
One.constants
# => [:Two]
One.instance_methods(true)
# => [:three]
One.instance_variables
# => [:@four]
One.class_variables
# => [:@@five]
global_variables.grep(/six/)
# => [:$six]
local_variables
# => [:seven]
\Symbol objects are different from String objects in that
\Symbol objects represent identifiers, while String objects
represent text or data.
== What's Here
First, what's elsewhere. \Class \Symbol:
- Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
- Includes {module Comparable}[rdoc-ref:Comparable@What-27s+Here].
Here, class \Symbol provides methods that are useful for:
- {Querying}[rdoc-ref:Symbol@Methods+for+Querying]
- {Comparing}[rdoc-ref:Symbol@Methods+for+Comparing]
- {Converting}[rdoc-ref:Symbol@Methods+for+Converting]
=== Methods for Querying
- ::all_symbols: Returns an array of the symbols currently in Ruby's symbol table.
- #=~: Returns the index of the first substring in symbol that matches a
given Regexp or other object; returns +nil+ if no match is found.
- #[], #slice : Returns a substring of symbol
determined by a given index, start/length, or range, or string.
- #empty?: Returns +true+ if +self.length+ is zero; +false+ otherwise.
- #encoding: Returns the Encoding object that represents the encoding
of symbol.
- #end_with?: Returns +true+ if symbol ends with
any of the given strings.
- #match: Returns a MatchData object if symbol
matches a given Regexp; +nil+ otherwise.
- #match?: Returns +true+ if symbol
matches a given Regexp; +false+ otherwise.
- #length, #size: Returns the number of characters in symbol.
- #start_with?: Returns +true+ if symbol starts with
any of the given strings.
=== Methods for Comparing
- #<=>: Returns -1, 0, or 1 as a given symbol is smaller than, equal to,
or larger than symbol.
- #==, #===: Returns +true+ if a given symbol has the same content and
encoding.
- #casecmp: Ignoring case, returns -1, 0, or 1 as a given
symbol is smaller than, equal to, or larger than symbol.
- #casecmp?: Returns +true+ if symbol is equal to a given symbol
after Unicode case folding; +false+ otherwise.
=== Methods for Converting
- #capitalize: Returns symbol with the first character upcased
and all other characters downcased.
- #downcase: Returns symbol with all characters downcased.
- #inspect: Returns the string representation of +self+ as a symbol literal.
- #name: Returns the frozen string corresponding to symbol.
- #succ, #next: Returns the symbol that is the successor to symbol.
- #swapcase: Returns symbol with all upcase characters downcased
and all downcase characters upcased.
- #to_proc: Returns a Proc object which responds to the method named by symbol.
- #to_s, #id2name: Returns the string corresponding to +self+.
- #to_sym, #intern: Returns +self+.
- #upcase: Returns symbol with all characters upcased.
Class Method Summary collapse
-
.all_symbols ⇒ Object
Returns an array of all symbols currently in Ruby’s symbol table:.
Instance Method Summary collapse
-
#<=>(object) ⇒ -1, ...
If
object
is a symbol, returns the equivalent ofsymbol.to_s <=> object.to_s
:. - #== ⇒ Object
- #=== ⇒ Object
-
#=~(object) ⇒ Integer?
Equivalent to
symbol.to_s =~ object
, including possible updates to global variables; see String#=~. -
#[](*args) ⇒ Object
Equivalent to
symbol.to_s[]
; see String#[]. -
#capitalize(*options) ⇒ Object
Equivalent to
sym.to_s.capitalize.to_sym
. -
#casecmp(object) ⇒ -1, ...
:include: doc/symbol/casecmp.rdoc.
-
#casecmp?(object) ⇒ true, ...
:include: doc/symbol/casecmp_p.rdoc.
-
#downcase(*options) ⇒ Object
Equivalent to
sym.to_s.downcase.to_sym
. -
#empty? ⇒ Boolean
Returns
true
ifself
is:''
,false
otherwise. -
#encoding ⇒ Encoding
Equivalent to
self.to_s.encoding
; see String#encoding. -
#end_with?(*strings) ⇒ Boolean
Equivalent to
self.to_s.end_with?
; see String#end_with?. -
#to_s ⇒ String
Returns a string representation of
self
(not including the leading colon):. -
#inspect ⇒ String
Returns a string representation of
self
(including the leading colon):. -
#length ⇒ Integer
Equivalent to
self.to_s.length
; see String#length. -
#match(*args) ⇒ Object
Equivalent to
self.to_s.match
, including possible updates to global variables; see String#match. -
#match?(pattern, offset) ⇒ Boolean
Equivalent to
sym.to_s.match?
; see String#match. -
#name ⇒ String
Returns a frozen string representation of
self
(not including the leading colon):. -
#succ ⇒ Object
Equivalent to
self.to_s.succ.to_sym
:. -
#length ⇒ Integer
Equivalent to
self.to_s.length
; see String#length. -
#slice(*args) ⇒ Object
Equivalent to
symbol.to_s[]
; see String#[]. -
#start_with?(*string_or_regexp) ⇒ Boolean
Equivalent to
self.to_s.start_with?
; see String#start_with?. -
#succ ⇒ Object
Equivalent to
self.to_s.succ.to_sym
:. -
#swapcase(*options) ⇒ Object
Equivalent to
sym.to_s.swapcase.to_sym
. -
#to_proc ⇒ Object
Returns a Proc object which calls the method with name of
self
on the first parameter and passes the remaining parameters to the method. -
#to_s ⇒ String
Returns a string representation of
self
(not including the leading colon):. -
#upcase(*options) ⇒ Object
Equivalent to
sym.to_s.upcase.to_sym
.
Methods included from Comparable
#<, #<=, #>, #>=, #between?, #clamp
Class Method Details
.all_symbols ⇒ Object
12068 12069 12070 12071 12072 |
# File 'string.c', line 12068
static VALUE
sym_all_symbols(VALUE _)
{
return rb_sym_all_symbols();
}
|
Instance Method Details
#<=>(object) ⇒ -1, ...
If object
is a symbol, returns the equivalent of symbol.to_s <=> object.to_s
:
:bar <=> :foo # => -1
:foo <=> :foo # => 0
:foo <=> :bar # => 1
Otherwise, returns nil
:
:foo <=> 'bar' # => nil
Related: String#<=>.
11778 11779 11780 11781 11782 11783 11784 11785 |
# File 'string.c', line 11778
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));
}
|
#== ⇒ Object
#=== ⇒ Object
#=~(object) ⇒ Integer?
Equivalent to symbol.to_s =~ object
, including possible updates to global variables; see String#=~.
11831 11832 11833 11834 11835 |
# File 'string.c', line 11831
static VALUE
sym_match(VALUE sym, VALUE other)
{
return rb_str_match(rb_sym2str(sym), other);
}
|
#[](index) ⇒ String? #[](start, length) ⇒ String? #[](range) ⇒ String? #[](regexp, capture = 0) ⇒ String? #[](substring) ⇒ String?
Equivalent to symbol.to_s[]
; see String#[].
11881 11882 11883 11884 11885 |
# File 'string.c', line 11881
static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
return rb_str_aref_m(argc, argv, rb_sym2str(sym));
}
|
#capitalize(*options) ⇒ Object
Equivalent to sym.to_s.capitalize.to_sym
.
See String#capitalize.
11958 11959 11960 11961 11962 |
# File 'string.c', line 11958
static VALUE
sym_capitalize(int argc, VALUE *argv, VALUE sym)
{
return rb_str_intern(rb_str_capitalize(argc, argv, rb_sym2str(sym)));
}
|
#casecmp(object) ⇒ -1, ...
:include: doc/symbol/casecmp.rdoc
11795 11796 11797 11798 11799 11800 11801 11802 |
# File 'string.c', line 11795
static VALUE
sym_casecmp(VALUE sym, VALUE other)
{
if (!SYMBOL_P(other)) {
return Qnil;
}
return str_casecmp(rb_sym2str(sym), rb_sym2str(other));
}
|
#casecmp?(object) ⇒ true, ...
:include: doc/symbol/casecmp_p.rdoc
11812 11813 11814 11815 11816 11817 11818 11819 |
# File 'string.c', line 11812
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(*options) ⇒ Object
Equivalent to sym.to_s.downcase.to_sym
.
See String#downcase.
Related: Symbol#upcase.
11942 11943 11944 11945 11946 |
# File 'string.c', line 11942
static VALUE
sym_downcase(int argc, VALUE *argv, VALUE sym)
{
return rb_str_intern(rb_str_downcase(argc, argv, rb_sym2str(sym)));
}
|
#empty? ⇒ Boolean
Returns true
if self
is :''
, false
otherwise.
11908 11909 11910 11911 11912 |
# File 'string.c', line 11908
static VALUE
sym_empty(VALUE sym)
{
return rb_str_empty(rb_sym2str(sym));
}
|
#encoding ⇒ Encoding
Equivalent to self.to_s.encoding
; see String#encoding.
12017 12018 12019 12020 12021 |
# File 'string.c', line 12017
static VALUE
sym_encoding(VALUE sym)
{
return rb_obj_encoding(rb_sym2str(sym));
}
|
#end_with?(*strings) ⇒ Boolean
Equivalent to self.to_s.end_with?
; see String#end_with?.
12003 12004 12005 12006 12007 |
# File 'string.c', line 12003
static VALUE
sym_end_with(int argc, VALUE *argv, VALUE sym)
{
return rb_str_end_with(argc, argv, rb_sym2str(sym));
}
|
#to_s ⇒ String
Returns a string representation of self
(not including the leading colon):
:foo.to_s # => "foo"
Related: Symbol#inspect, Symbol#name.
11725 11726 11727 11728 11729 |
# File 'string.c', line 11725
VALUE
rb_sym_to_s(VALUE sym)
{
return str_new_shared(rb_cString, rb_sym2str(sym));
}
|
#inspect ⇒ String
Returns a string representation of self
(including the leading colon):
:foo.inspect # => ":foo"
Related: Symbol#to_s, Symbol#name.
11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 |
# File 'string.c', line 11683
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);
VALUE orig_str = str;
RSTRING_GETMEM(orig_str, ptr, len);
str = rb_enc_str_new(0, len + 1, enc);
dest = RSTRING_PTR(str);
memcpy(dest + 1, ptr, len);
RB_GC_GUARD(orig_str);
}
dest[0] = ':';
return str;
}
|
#length ⇒ Integer
Equivalent to self.to_s.length
; see String#length.
11894 11895 11896 11897 11898 |
# File 'string.c', line 11894
static VALUE
sym_length(VALUE sym)
{
return rb_str_length(rb_sym2str(sym));
}
|
#match(pattern, offset = 0) ⇒ MatchData? #match(pattern, offset = 0) {|matchdata| ... } ⇒ Object
Equivalent to self.to_s.match
, including possible updates to global variables; see String#match.
11848 11849 11850 11851 11852 |
# File 'string.c', line 11848
static VALUE
sym_match_m(int argc, VALUE *argv, VALUE sym)
{
return rb_str_match_m(argc, argv, rb_sym2str(sym));
}
|
#match?(pattern, offset) ⇒ Boolean
Equivalent to sym.to_s.match?
; see String#match.
11863 11864 11865 11866 11867 |
# File 'string.c', line 11863
static VALUE
sym_match_m_p(int argc, VALUE *argv, VALUE sym)
{
return rb_str_match_m_p(argc, argv, sym);
}
|
#name ⇒ String
Returns a frozen string representation of self
(not including the leading colon):
:foo.name # => "foo"
:foo.name.frozen? # => true
Related: Symbol#to_s, Symbol#inspect.
952 953 954 955 956 957 958 959 960 961 |
# File 'symbol.c', line 952
VALUE
rb_sym2str(VALUE sym)
{
if (DYNAMIC_SYM_P(sym)) {
return RSYMBOL(sym)->fstr;
}
else {
return rb_id2str(STATIC_SYM2ID(sym));
}
}
|
#succ ⇒ Object
Equivalent to self.to_s.succ.to_sym
:
:foo.succ # => :fop
Related: String#succ.
11754 11755 11756 11757 11758 |
# File 'string.c', line 11754
static VALUE
sym_succ(VALUE sym)
{
return rb_str_intern(rb_str_succ(rb_sym2str(sym)));
}
|
#length ⇒ Integer
Equivalent to self.to_s.length
; see String#length.
11894 11895 11896 11897 11898 |
# File 'string.c', line 11894
static VALUE
sym_length(VALUE sym)
{
return rb_str_length(rb_sym2str(sym));
}
|
#[](index) ⇒ String? #[](start, length) ⇒ String? #[](range) ⇒ String? #[](regexp, capture = 0) ⇒ String? #[](substring) ⇒ String?
Equivalent to symbol.to_s[]
; see String#[].
11881 11882 11883 11884 11885 |
# File 'string.c', line 11881
static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
return rb_str_aref_m(argc, argv, rb_sym2str(sym));
}
|
#start_with?(*string_or_regexp) ⇒ Boolean
Equivalent to self.to_s.start_with?
; see String#start_with?.
11988 11989 11990 11991 11992 |
# File 'string.c', line 11988
static VALUE
sym_start_with(int argc, VALUE *argv, VALUE sym)
{
return rb_str_start_with(argc, argv, rb_sym2str(sym));
}
|
#succ ⇒ Object
Equivalent to self.to_s.succ.to_sym
:
:foo.succ # => :fop
Related: String#succ.
11754 11755 11756 11757 11758 |
# File 'string.c', line 11754
static VALUE
sym_succ(VALUE sym)
{
return rb_str_intern(rb_str_succ(rb_sym2str(sym)));
}
|
#swapcase(*options) ⇒ Object
Equivalent to sym.to_s.swapcase.to_sym
.
See String#swapcase.
11974 11975 11976 11977 11978 |
# File 'string.c', line 11974
static VALUE
sym_swapcase(int argc, VALUE *argv, VALUE sym)
{
return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym)));
}
|
#to_proc ⇒ Object
Returns a Proc object which calls the method with name of self
on the first parameter and passes the remaining parameters to the method.
proc = :to_s.to_proc # => #<Proc:0x000001afe0e48680(&:to_s) (lambda)>
proc.call(1000) # => "1000"
proc.call(1000, 16) # => "3e8"
(1..3).collect(&:to_s) # => ["1", "2", "3"]
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 |
# File 'proc.c', line 1502
VALUE
rb_sym_to_proc(VALUE sym)
{
static VALUE sym_proc_cache = Qfalse;
enum {SYM_PROC_CACHE_SIZE = 67};
VALUE proc;
long index;
ID id;
if (!sym_proc_cache) {
sym_proc_cache = rb_ary_hidden_new(SYM_PROC_CACHE_SIZE * 2);
rb_gc_register_mark_object(sym_proc_cache);
rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
}
id = SYM2ID(sym);
index = (id % SYM_PROC_CACHE_SIZE) << 1;
if (RARRAY_AREF(sym_proc_cache, index) == sym) {
return RARRAY_AREF(sym_proc_cache, index + 1);
}
else {
proc = sym_proc_new(rb_cProc, ID2SYM(id));
RARRAY_ASET(sym_proc_cache, index, sym);
RARRAY_ASET(sym_proc_cache, index + 1, proc);
return proc;
}
}
|
#to_s ⇒ String
Returns a string representation of self
(not including the leading colon):
:foo.to_s # => "foo"
Related: Symbol#inspect, Symbol#name.
11725 11726 11727 11728 11729 |
# File 'string.c', line 11725
VALUE
rb_sym_to_s(VALUE sym)
{
return str_new_shared(rb_cString, rb_sym2str(sym));
}
|
#upcase(*options) ⇒ Object
Equivalent to sym.to_s.upcase.to_sym
.
See String#upcase.
11924 11925 11926 11927 11928 |
# File 'string.c', line 11924
static VALUE
sym_upcase(int argc, VALUE *argv, VALUE sym)
{
return rb_str_intern(rb_str_upcase(argc, argv, rb_sym2str(sym)));
}
|