Symbol
objects represent named identifiers inside the Ruby interpreter.
You can create a Symbol object explicitly with:
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 $f2.object_id $f3.object_id
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 One.instance_methods(true) One.instance_variables One.class_variables global_variables.grep(/six/) local_variables
Symbol
objects are different from String
objects in that Symbol
objects represent identifiers, while String
objects represent text or data.
First, whatâs elsewhere. Class Symbol:
Inherits from class Object.
Includes module Comparable.
Here, class Symbol provides methods that are useful for:
Methods for Querying¶ ↑::all_symbols
Returns an array of the symbols currently in Rubyâs symbol table.
empty?
Returns true
if self.length
is zero; false
otherwise.
end_with?
Returns true
if symbol ends with any of the given strings.
start_with?
Returns true
if symbol starts with any of the given strings.
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.
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.
swapcase
Returns symbol with all upcase characters downcased and all downcase characters upcased.
upcase
Returns symbol with all characters upcased.
all_symbols → array click to toggle source
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, :$>]
static VALUE sym_all_symbols(VALUE _) { return rb_sym_all_symbols(); }
json_create(o) click to toggle source
Deserializes JSON
string by converting the string
value stored in the object to a Symbol
def self.json_create(o) o['s'].to_sym endPublic Instance Methods
symbol <=> other_symbol → -1, 0, +1, or nil click to toggle source
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.
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)); }
sym == obj → true or false click to toggle source
EqualityâIf sym and obj are exactly the same symbol, returns true
.
#define sym_equal rb_obj_equal
===(p1)
EqualityâIf sym and obj are exactly the same symbol, returns true
.
sym =~ obj → integer or nil click to toggle source
Returns sym.to_s =~ obj
.
static VALUE sym_match(VALUE sym, VALUE other) { return rb_str_match(rb_sym2str(sym), other); }
sym[idx] → char click to toggle source
sym[b, n] → string
Returns sym.to_s[]
.
static VALUE sym_aref(int argc, VALUE *argv, VALUE sym) { return rb_str_aref_m(argc, argv, rb_sym2str(sym)); }
as_json(*) click to toggle source
Returns a hash, that will be turned into a JSON
object and represent this object.
def as_json(*) { JSON.create_id => self.class.name, 's' => to_s, } end
capitalize(*options) → symbol click to toggle source
Equivalent to sym.to_s.capitalize.to_sym
.
See String#capitalize
.
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, 0, 1, or nil click to toggle source
Case-insensitive version of Symbol#<=>:
:aBcDeF.casecmp(:abcde) :aBcDeF.casecmp(:abcdef) :aBcDeF.casecmp(:abcdefg) :abcdef.casecmp(:ABCDEF)
Returns nil
if the two symbols have incompatible encodings, or if other_symbol
is not a symbol:
sym = "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym other_sym = :"\u{c4 d6 dc}" sym.casecmp(other_sym) :foo.casecmp(2)
Currently, case-insensitivity only works on characters A-Z/a-z, not all of Unicode. This is different from Symbol#casecmp?
.
Related: Symbol#casecmp?
.
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, false, or nil click to toggle source
Returns true
if sym
and other_symbol
are equal after Unicode case folding, false
if they are not equal:
:aBcDeF.casecmp?(:abcde) :aBcDeF.casecmp?(:abcdef) :aBcDeF.casecmp?(:abcdefg) :abcdef.casecmp?(:ABCDEF) :"\u{e4 f6 fc}".casecmp?(:"\u{c4 d6 dc}")
Returns nil
if the two symbols have incompatible encodings, or if other_symbol
is not a symbol:
sym = "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym other_sym = :"\u{c4 d6 dc}" sym.casecmp?(other_sym) :foo.casecmp?(2)
See Case Mapping.
Related: Symbol#casecmp
.
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) → symbol click to toggle source
Equivalent to sym.to_s.downcase.to_sym
.
See String#downcase
.
Related: Symbol#upcase
.
static VALUE sym_downcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_downcase(argc, argv, rb_sym2str(sym))); }
empty? → true or false click to toggle source
Returns whether sym is :ââ or not.
static VALUE sym_empty(VALUE sym) { return rb_str_empty(rb_sym2str(sym)); }
encoding → encoding click to toggle source
Returns the Encoding
object that represents the encoding of sym.
static VALUE sym_encoding(VALUE sym) { return rb_obj_encoding(rb_sym2str(sym)); }
end_with?([suffixes]+) → true or false click to toggle source
Returns true if sym
ends with one of the suffixes
given.
:hello.end_with?("ello") :hello.end_with?("heaven", "ello") :hello.end_with?("heaven", "paradise")
static VALUE sym_end_with(int argc, VALUE *argv, VALUE sym) { return rb_str_end_with(argc, argv, rb_sym2str(sym)); }
id2name → string
Returns the name or string corresponding to sym.
:fred.id2name :ginger.to_s
Note that this string is not frozen (unlike the symbol itself). To get a frozen string, use name
.
inspect → string click to toggle source
Returns the representation of sym as a symbol literal.
:fred.inspect
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; }
intern → sym click to toggle source
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
static VALUE sym_to_sym(VALUE sym) { return sym; }
length → integer click to toggle source
Same as sym.to_s.length
.
static VALUE sym_length(VALUE sym) { return rb_str_length(rb_sym2str(sym)); }
match(pattern) → matchdata or nil click to toggle source
match(pattern, pos) → matchdata or nil
Returns sym.to_s.match
.
static VALUE sym_match_m(int argc, VALUE *argv, VALUE sym) { return rb_str_match_m(argc, argv, rb_sym2str(sym)); }
match?(pattern) → true or false click to toggle source
match?(pattern, pos) → true or false
Returns sym.to_s.match?
.
static VALUE sym_match_m_p(int argc, VALUE *argv, VALUE sym) { return rb_str_match_m_p(argc, argv, sym); }
name → string click to toggle source
Returns the name or string corresponding to sym. Unlike to_s
, the returned string is frozen.
:fred.name :fred.name.frozen? :fred.to_s :fred.to_s.frozen?
VALUE rb_sym2str(VALUE sym) { }
next()
Same as sym.to_s.succ.intern
.
size → integer
Same as sym.to_s.length
.
slice(idx) → char
slice(b, n) → string
Returns sym.to_s[]
.
start_with?([prefixes]+) → true or false click to toggle source
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") :hello.start_with?(/H/i) :hello.start_with?("heaven", "hell") :hello.start_with?("heaven", "paradise")
static VALUE sym_start_with(int argc, VALUE *argv, VALUE sym) { return rb_str_start_with(argc, argv, rb_sym2str(sym)); }
succ click to toggle source
Same as sym.to_s.succ.intern
.
static VALUE sym_succ(VALUE sym) { return rb_str_intern(rb_str_succ(rb_sym2str(sym))); }
swapcase(*options) → symbol click to toggle source
Equivalent to sym.to_s.swapcase.to_sym
.
See String#swapcase
.
static VALUE sym_swapcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym))); }
to_json(*a) click to toggle source
Stores class name (Symbol
) with String
representation of Symbol
as a JSON
string.
def to_json(*a) as_json.to_json(*a) end
to_proc click to toggle source
Returns a Proc object which responds to the given method by sym.
(1..3).collect(&:to_s)
VALUE rb_sym_to_proc(VALUE sym) { }
to_s → string click to toggle source
Returns the name or string corresponding to sym.
:fred.id2name :ginger.to_s
Note that this string is not frozen (unlike the symbol itself). To get a frozen string, use name
.
VALUE rb_sym_to_s(VALUE sym) { return str_new_shared(rb_cString, rb_sym2str(sym)); }
to_sym → sym
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
upcase(*options) → symbol click to toggle source
Equivalent to sym.to_s.upcase.to_sym
.
See String#upcase
.
static VALUE sym_upcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_upcase(argc, argv, rb_sym2str(sym))); }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4