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.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
symbolwithother_symbolafter calling #to_s on each of the symbols. -
#==(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. -
#=~(other) ⇒ Object
Returns
sym.to_s =~ obj. -
#[](*args) ⇒ 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_symreturns theSymbolcorresponding to an object. -
#length ⇒ Object
Same as
sym.to_s.length. -
#match(other) ⇒ Object
Returns
sym.to_s =~ obj. -
#succ ⇒ Object
Same as
sym.to_s.succ.intern. -
#size ⇒ Object
Same as
sym.to_s.length. -
#slice(*args) ⇒ 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_symreturns theSymbolcorresponding 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, :$>]
10638 10639 10640 10641 10642 10643 10644 10645 |
# File 'parse.y', line 10638 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_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.
8545 8546 8547 8548 8549 8550 8551 8552 |
# File 'string.c', line 8545 static VALUE sym_cmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return rb_str_cmp_m(rb_sym_to_s(sym), rb_sym_to_s(other)); } |
#==(obj) ⇒ Boolean
Equality—If sym and obj are exactly the same symbol, returns true.
8323 8324 8325 8326 8327 8328 |
# File 'string.c', line 8323 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.
8323 8324 8325 8326 8327 8328 |
# File 'string.c', line 8323 static VALUE sym_equal(VALUE sym1, VALUE sym2) { if (sym1 == sym2) return Qtrue; return Qfalse; } |
#=~(obj) ⇒ Fixnum? #match(obj) ⇒ Fixnum?
Returns sym.to_s =~ obj.
8579 8580 8581 8582 8583 |
# File 'string.c', line 8579 static VALUE sym_match(VALUE sym, VALUE other) { return rb_str_match(rb_sym_to_s(sym), other); } |
#[](idx) ⇒ String #[](b, n) ⇒ String #slice(idx) ⇒ String #slice(b, n) ⇒ String
Returns sym.to_s[].
8595 8596 8597 8598 8599 |
# File 'string.c', line 8595 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.
8661 8662 8663 8664 8665 |
# File 'string.c', line 8661 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#<=>.
8562 8563 8564 8565 8566 8567 8568 8569 |
# File 'string.c', line 8562 static VALUE sym_casecmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return rb_str_casecmp(rb_sym_to_s(sym), rb_sym_to_s(other)); } |
#downcase ⇒ Object
Same as sym.to_s.downcase.intern.
8648 8649 8650 8651 8652 |
# File 'string.c', line 8648 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.
8622 8623 8624 8625 8626 |
# File 'string.c', line 8622 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.
8687 8688 8689 8690 8691 |
# File 'string.c', line 8687 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"
8441 8442 8443 8444 8445 8446 8447 |
# File 'string.c', line 8441 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"
8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 |
# File 'string.c', line 8399 static VALUE sym_inspect(VALUE sym) { VALUE str; const char *ptr; long len; ID id = SYM2ID(sym); char *dest; sym = rb_id2str(id); if (!rb_str_symname_p(sym)) { 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] = ':'; } else { rb_encoding *enc = STR_ENC_GET(sym); ptr = RSTRING_PTR(sym); len = RSTRING_LEN(sym); str = rb_enc_str_new(0, len + 1, enc); dest = RSTRING_PTR(str); dest[0] = ':'; memcpy(dest + 1, ptr, len); } 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.
8460 8461 8462 8463 8464 |
# File 'string.c', line 8460 static VALUE sym_to_sym(VALUE sym) { return sym; } |
#length ⇒ Integer #size ⇒ Integer
Same as sym.to_s.length.
8609 8610 8611 8612 8613 |
# File 'string.c', line 8609 static VALUE sym_length(VALUE sym) { return rb_str_length(rb_id2str(SYM2ID(sym))); } |
#=~(obj) ⇒ Fixnum? #match(obj) ⇒ Fixnum?
Returns sym.to_s =~ obj.
8579 8580 8581 8582 8583 |
# File 'string.c', line 8579 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.
8525 8526 8527 8528 8529 |
# File 'string.c', line 8525 static VALUE sym_succ(VALUE sym) { return rb_str_intern(rb_str_succ(rb_sym_to_s(sym))); } |
#length ⇒ Integer #size ⇒ Integer
Same as sym.to_s.length.
8609 8610 8611 8612 8613 |
# File 'string.c', line 8609 static VALUE sym_length(VALUE sym) { return rb_str_length(rb_id2str(SYM2ID(sym))); } |
#[](idx) ⇒ String #[](b, n) ⇒ String #slice(idx) ⇒ String #slice(b, n) ⇒ String
Returns sym.to_s[].
8595 8596 8597 8598 8599 |
# File 'string.c', line 8595 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.
8525 8526 8527 8528 8529 |
# File 'string.c', line 8525 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.
8674 8675 8676 8677 8678 |
# File 'string.c', line 8674 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"]
8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 |
# File 'string.c', line 8487 static VALUE sym_to_proc(VALUE sym) { static VALUE sym_proc_cache = Qfalse; enum {SYM_PROC_CACHE_SIZE = 67}; VALUE proc; long id, index; VALUE *aryp; if (!sym_proc_cache) { sym_proc_cache = rb_ary_tmp_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; aryp = RARRAY_PTR(sym_proc_cache); if (aryp[index] == sym) { return aryp[index + 1]; } else { proc = rb_proc_new(sym_call, (VALUE)id); aryp[index] = sym; aryp[index + 1] = proc; return proc; } } |
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
8441 8442 8443 8444 8445 8446 8447 |
# File 'string.c', line 8441 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.
8460 8461 8462 8463 8464 |
# File 'string.c', line 8460 static VALUE sym_to_sym(VALUE sym) { return sym; } |
#upcase ⇒ Object
Same as sym.to_s.upcase.intern.
8635 8636 8637 8638 8639 |
# File 'string.c', line 8635 static VALUE sym_upcase(VALUE sym) { return rb_str_intern(rb_str_upcase(rb_id2str(SYM2ID(sym)))); } |