Class: Symbol
Overview
********************************************************************
Symbol objects represent names 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 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
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_symbol) ⇒ -1, ...
Compares
symbol
withother_symbol
after calling #to_s on each of the symbols. - #== ⇒ Object
- #=== ⇒ Object
-
#=~(obj) ⇒ Integer?
Returns
sym.to_s =~ obj
. -
#[](*args) ⇒ Object
Returns
sym.to_s[]
. -
#capitalize(*args) ⇒ Object
Same as
sym.to_s.capitalize.intern
. -
#casecmp(other_symbol) ⇒ -1, ...
Case-insensitive version of Symbol#<=>.
-
#casecmp?(other_symbol) ⇒ true, ...
Returns
true
ifsym
andother_symbol
are equal after Unicode case folding,false
if they are not equal. -
#downcase(*args) ⇒ Object
Same as
sym.to_s.downcase.intern
. -
#empty? ⇒ Boolean
Returns whether sym is :“” or not.
-
#encoding ⇒ Encoding
Returns the Encoding object that represents the encoding of sym.
-
#end_with?([suffixes]) ⇒ Boolean
Returns true if
sym
ends with one of thesuffixes
given. -
#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 the Symbol corresponding to an object. -
#length ⇒ Object
Same as
sym.to_s.length
. -
#match(*args) ⇒ Object
Returns
sym.to_s.match
. -
#match?(*args) ⇒ Object
Returns
sym.to_s.match?
. -
#succ ⇒ Object
Same as
sym.to_s.succ.intern
. -
#size ⇒ Object
Same as
sym.to_s.length
. -
#slice(*args) ⇒ Object
Returns
sym.to_s[]
. -
#start_with?([prefixes]) ⇒ Boolean
Returns true if
sym
starts with one of theprefixes
given. -
#succ ⇒ Object
Same as
sym.to_s.succ.intern
. -
#swapcase(*args) ⇒ Object
Same as
sym.to_s.swapcase.intern
. -
#to_proc ⇒ Object
Returns a Proc object which responds 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 the Symbol corresponding to an object. -
#upcase(*args) ⇒ Object
Same as
sym.to_s.upcase.intern
.
Methods included from Comparable
#<, #<=, #>, #>=, #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, :$>]
11196 11197 11198 11199 11200 |
# File 'string.c', line 11196
static VALUE
sym_all_symbols(VALUE _)
{
return rb_sym_all_symbols();
}
|
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.
10886 10887 10888 10889 10890 10891 10892 10893 |
# File 'string.c', line 10886
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
#=~(obj) ⇒ Integer?
Returns sym.to_s =~ obj
.
10960 10961 10962 10963 10964 |
# File 'string.c', line 10960
static VALUE
sym_match(VALUE sym, VALUE other)
{
return rb_str_match(rb_sym2str(sym), other);
}
|
#[](idx) ⇒ String #[](b, n) ⇒ String #slice(idx) ⇒ String #slice(b, n) ⇒ String
Returns sym.to_s[]
.
11004 11005 11006 11007 11008 |
# File 'string.c', line 11004
static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
return rb_str_aref_m(argc, argv, rb_sym2str(sym));
}
|
#capitalize ⇒ Object #capitalize([options]) ⇒ Object
Same as sym.to_s.capitalize.intern
.
11073 11074 11075 11076 11077 |
# File 'string.c', line 11073
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 Symbol#<=>. Currently, case-insensitivity only works on characters A-Z/a-z, not all of Unicode. This is different from Symbol#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
10915 10916 10917 10918 10919 10920 10921 10922 |
# File 'string.c', line 10915
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
10944 10945 10946 10947 10948 10949 10950 10951 |
# File 'string.c', line 10944
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 ⇒ Object #downcase([options]) ⇒ Object
Same as sym.to_s.downcase.intern
.
11059 11060 11061 11062 11063 |
# File 'string.c', line 11059
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 whether sym is :“” or not.
11031 11032 11033 11034 11035 |
# File 'string.c', line 11031
static VALUE
sym_empty(VALUE sym)
{
return rb_str_empty(rb_sym2str(sym));
}
|
#encoding ⇒ Encoding
Returns the Encoding object that represents the encoding of sym.
11140 11141 11142 11143 11144 |
# File 'string.c', line 11140
static VALUE
sym_encoding(VALUE sym)
{
return rb_obj_encoding(rb_sym2str(sym));
}
|
#end_with?([suffixes]) ⇒ Boolean
Returns true if sym
ends with one of the suffixes
given.
:hello.end_with?("ello") #=> true
# returns true if one of the +suffixes+ matches.
:hello.end_with?("heaven", "ello") #=> true
:hello.end_with?("heaven", "paradise") #=> false
11127 11128 11129 11130 11131 |
# File 'string.c', line 11127
static VALUE
sym_end_with(int argc, VALUE *argv, VALUE sym)
{
return rb_str_end_with(argc, argv, rb_sym2str(sym));
}
|
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
:ginger.to_s #=> "ginger"
10807 10808 10809 10810 10811 |
# File 'string.c', line 10807
VALUE
rb_sym_to_s(VALUE sym)
{
return str_new_shared(rb_cString, rb_sym2str(sym));
}
|
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
:fred.inspect #=> ":fred"
10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 |
# File 'string.c', line 10768
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 ⇒ 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.
10824 10825 10826 10827 10828 |
# File 'string.c', line 10824
static VALUE
sym_to_sym(VALUE sym)
{
return sym;
}
|
#length ⇒ Integer #size ⇒ Integer
Same as sym.to_s.length
.
11018 11019 11020 11021 11022 |
# File 'string.c', line 11018
static VALUE
sym_length(VALUE sym)
{
return rb_str_length(rb_sym2str(sym));
}
|
#match(pattern) ⇒ MatchData? #match(pattern, pos) ⇒ MatchData?
Returns sym.to_s.match
.
10974 10975 10976 10977 10978 |
# File 'string.c', line 10974
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
Returns sym.to_s.match?
.
10988 10989 10990 10991 10992 |
# File 'string.c', line 10988
static VALUE
sym_match_m_p(int argc, VALUE *argv, VALUE sym)
{
return rb_str_match_m_p(argc, argv, sym);
}
|
#succ ⇒ Object
Same as sym.to_s.succ.intern
.
10866 10867 10868 10869 10870 |
# File 'string.c', line 10866
static VALUE
sym_succ(VALUE sym)
{
return rb_str_intern(rb_str_succ(rb_sym2str(sym)));
}
|
#length ⇒ Integer #size ⇒ Integer
Same as sym.to_s.length
.
11018 11019 11020 11021 11022 |
# File 'string.c', line 11018
static VALUE
sym_length(VALUE sym)
{
return rb_str_length(rb_sym2str(sym));
}
|
#[](idx) ⇒ String #[](b, n) ⇒ String #slice(idx) ⇒ String #slice(b, n) ⇒ String
Returns sym.to_s[]
.
11004 11005 11006 11007 11008 |
# File 'string.c', line 11004
static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
return rb_str_aref_m(argc, argv, rb_sym2str(sym));
}
|
#start_with?([prefixes]) ⇒ Boolean
Returns true if sym
starts with one of the prefixes
given. Each of the prefixes
should be a String or a Regexp.
:hello.start_with?("hell") #=> true
:hello.start_with?(/H/i) #=> true
# returns true if one of the prefixes matches.
:hello.start_with?("heaven", "hell") #=> true
:hello.start_with?("heaven", "paradise") #=> false
11108 11109 11110 11111 11112 |
# File 'string.c', line 11108
static VALUE
sym_start_with(int argc, VALUE *argv, VALUE sym)
{
return rb_str_start_with(argc, argv, rb_sym2str(sym));
}
|
#succ ⇒ Object
Same as sym.to_s.succ.intern
.
10866 10867 10868 10869 10870 |
# File 'string.c', line 10866
static VALUE
sym_succ(VALUE sym)
{
return rb_str_intern(rb_str_succ(rb_sym2str(sym)));
}
|
#swapcase ⇒ Object #swapcase([options]) ⇒ Object
Same as sym.to_s.swapcase.intern
.
11087 11088 11089 11090 11091 |
# File 'string.c', line 11087
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 responds to the given method by sym.
(1..3).collect(&:to_s) #=> ["1", "2", "3"]
10852 10853 10854 10855 |
# File 'string.c', line 10852 VALUE rb_sym_to_proc(VALUE sym) { } |
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
:ginger.to_s #=> "ginger"
10807 10808 10809 10810 10811 |
# File 'string.c', line 10807
VALUE
rb_sym_to_s(VALUE sym)
{
return str_new_shared(rb_cString, rb_sym2str(sym));
}
|