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
Methods
#
A
C
D
E
I
J
L
M
N
S
T
U
Y
Included Modules
Class Public methods
Symbol.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, :$>]
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;
}
json_create(o)
    # File ext/json/lib/json/add/core.rb, line 18
18:   def self.json_create(o)
19:     o['s'].to_sym
20:   end
yaml_new( klass, tag, val )
     # File ext/syck/lib/syck/rubytypes.rb, line 190
190:     def Symbol.yaml_new( klass, tag, val )
191:         if String === val
192:             val = YAML::load( val ) if val =~ /\A(["']).*\1\z/
193:             val.intern
194:         else
195:             raise YAML::TypeError, "Invalid Symbol: " + val.inspect
196:         end
197:     end
Instance Public methods
  str <=> other       -> -1, 0, +1 or nil

Compares sym with other in string form.

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));
}
sym == obj → true or false

Equality—If sym and obj are exactly the same symbol, returns true.

static VALUE
sym_equal(VALUE sym1, VALUE sym2)
{
    if (sym1 == sym2) return Qtrue;
    return Qfalse;
}
sym == obj → true or false

Equality—If sym and obj are exactly the same symbol, returns true.

static VALUE
sym_equal(VALUE sym1, VALUE sym2)
{
    if (sym1 == sym2) return Qtrue;
    return Qfalse;
}
sym =~ obj → fixnum or nil

Returns sym.to_s =~ obj.

static VALUE
sym_match(VALUE sym, VALUE other)
{
    return rb_str_match(rb_sym_to_s(sym), other);
}
sym[idx] → char sym[b, n] → char

Returns sym.to_s[].

static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
    return rb_str_aref_m(argc, argv, rb_sym_to_s(sym));
}
sym.capitalize → symbol

Same as sym.to_s.capitalize.intern.

static VALUE
sym_capitalize(VALUE sym)
{
    return rb_str_intern(rb_str_capitalize(rb_id2str(SYM2ID(sym))));
}
  sym.casecmp(other)  -> -1, 0, +1 or nil

Case-insensitive version of Symbol#<=>.

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));
}
dclone()
    # File lib/rexml/xpath_parser.rb, line 13
13:   def dclone ; self ; end
sym.downcase → symbol

Same as sym.to_s.downcase.intern.

static VALUE
sym_downcase(VALUE sym)
{
    return rb_str_intern(rb_str_downcase(rb_id2str(SYM2ID(sym))));
}
sym.empty? → true or false

Returns that sym is :”“ or not.

static VALUE
sym_empty(VALUE sym)
{
    return rb_str_empty(rb_id2str(SYM2ID(sym)));
}
sym.encoding → encoding

Returns the Encoding object that represents the encoding of sym.

static VALUE
sym_encoding(VALUE sym)
{
    return rb_obj_encoding(rb_id2str(SYM2ID(sym)));
}
sym.id2name → string sym.to_s → string

Returns the name or string corresponding to sym.

   :fred.id2name   #=> "fred"
VALUE
rb_sym_to_s(VALUE sym)
{
    ID id = SYM2ID(sym);

    return str_new3(rb_cString, rb_id2str(id));
}
sym.inspect → string

Returns the representation of sym as a symbol literal.

   :fred.inspect   #=> ":fred"
static VALUE
sym_inspect(VALUE sym)
{
    VALUE str;
    ID id = SYM2ID(sym);
    rb_encoding *enc;
    const char *ptr;
    long len;
    char *dest;
    rb_encoding *resenc = rb_default_internal_encoding();

    if (resenc == NULL) resenc = rb_default_external_encoding();
    sym = rb_id2str(id);
    enc = STR_ENC_GET(sym);
    ptr = RSTRING_PTR(sym);
    len = RSTRING_LEN(sym);
    if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) ||
        !rb_enc_symname_p(ptr, enc) || !sym_printable(ptr, ptr + len, enc)) {
        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 {
        char *dest;
        str = rb_enc_str_new(0, len + 1, enc);
        dest = RSTRING_PTR(str);
        dest[0] = ':';
        memcpy(dest + 1, ptr, len);
    }
    return str;
}
sym.to_sym → 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.

static VALUE
sym_to_sym(VALUE sym)
{
    return sym;
}
sym.length → integer

Same as sym.to_s.length.

static VALUE
sym_length(VALUE sym)
{
    return rb_str_length(rb_id2str(SYM2ID(sym)));
}
sym =~ obj → fixnum or nil

Returns sym.to_s =~ obj.

static VALUE
sym_match(VALUE sym, VALUE other)
{
    return rb_str_match(rb_sym_to_s(sym), other);
}
  sym.succ

Same as sym.to_s.succ.intern.

static VALUE
sym_succ(VALUE sym)
{
    return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}
sym.length → integer

Same as sym.to_s.length.

static VALUE
sym_length(VALUE sym)
{
    return rb_str_length(rb_id2str(SYM2ID(sym)));
}
sym[idx] → char sym[b, n] → char

Returns sym.to_s[].

static VALUE
sym_aref(int argc, VALUE *argv, VALUE sym)
{
    return rb_str_aref_m(argc, argv, rb_sym_to_s(sym));
}
  sym.succ

Same as sym.to_s.succ.intern.

static VALUE
sym_succ(VALUE sym)
{
    return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}
sym.swapcase → symbol

Same as sym.to_s.swapcase.intern.

static VALUE
sym_swapcase(VALUE sym)
{
    return rb_str_intern(rb_str_swapcase(rb_id2str(SYM2ID(sym))));
}
to_json(*a)
    # File ext/json/lib/json/add/rails.rb, line 32
32:   def to_json(*a)
33:     to_s.to_json(*a)
34:   end
to_json(*a)
    # File ext/json/lib/json/add/core.rb, line 11
11:   def to_json(*a)
12:     {
13:       JSON.create_id => self.class.name,
14:       's' => to_s,
15:     }.to_json(*a)
16:   end
sym.to_proc

Returns a Proc object which respond to the given method by sym.

  (1..3).collect(&:to_s)  #=> ["1", "2", "3"]
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;
    }
}
sym.id2name → string sym.to_s → string

Returns the name or string corresponding to sym.

   :fred.id2name   #=> "fred"
VALUE
rb_sym_to_s(VALUE sym)
{
    ID id = SYM2ID(sym);

    return str_new3(rb_cString, rb_id2str(id));
}
sym.to_sym → 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.

static VALUE
sym_to_sym(VALUE sym)
{
    return sym;
}
to_yaml( opts = {} )
     # File ext/syck/lib/syck/rubytypes.rb, line 198
198:         def to_yaml( opts = {} )
199:         return super unless YAML::ENGINE.syck?
200:                 YAML::quick_emit( nil, opts ) do |out|
201:             out.scalar( "tag:yaml.org,2002:str", self.inspect, :plain )
202:         end
203:         end
sym.upcase → symbol

Same as sym.to_s.upcase.intern.

static VALUE
sym_upcase(VALUE sym)
{
    return rb_str_intern(rb_str_upcase(rb_id2str(SYM2ID(sym))));
}