Class: Symbol

Inherits:
Object show all
Includes:
Comparable
Defined in:
string.c,
string.c

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

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #>, #>=, #between?

Class Method Details

.all_symbolsArray

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, :$>]

Returns:


10402
10403
10404
10405
10406
10407
10408
10409
# File 'parse.y', line 10402

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) ⇒ -1, ...

Compares sym with other in string form.

Returns:

  • (-1, 0, +1, nil)

7954
7955
7956
7957
7958
7959
7960
7961
# File 'string.c', line 7954

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.

Returns:

  • (Boolean)

7740
7741
7742
7743
7744
7745
# File 'string.c', line 7740

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.

Returns:

  • (Boolean)

7740
7741
7742
7743
7744
7745
# File 'string.c', line 7740

static VALUE
sym_equal(VALUE sym1, VALUE sym2)
{
    if (sym1 == sym2) return Qtrue;
    return Qfalse;
}

#=~(obj) ⇒ Fixnum?

Returns sym.to_s =~ obj.

Returns:


7987
7988
7989
7990
7991
# File 'string.c', line 7987

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

#[](idx) ⇒ String #[](b, n) ⇒ String

Returns sym.to_s[].

Overloads:


8001
8002
8003
8004
8005
# File 'string.c', line 8001

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

#capitalizeObject

Same as sym.to_s.capitalize.intern.


8066
8067
8068
8069
8070
# File 'string.c', line 8066

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#<=>.

Returns:

  • (-1, 0, +1, nil)

7971
7972
7973
7974
7975
7976
7977
7978
# File 'string.c', line 7971

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));
}

#downcaseObject

Same as sym.to_s.downcase.intern.


8053
8054
8055
8056
8057
# File 'string.c', line 8053

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.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

8027
8028
8029
8030
8031
# File 'string.c', line 8027

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

#encodingEncoding

Returns the Encoding object that represents the encoding of sym.

Returns:


8092
8093
8094
8095
8096
# File 'string.c', line 8092

static VALUE
sym_encoding(VALUE sym)
{
    return rb_obj_encoding(rb_id2str(SYM2ID(sym)));
}

#id2nameString #to_sString

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"

Overloads:


7856
7857
7858
7859
7860
7861
7862
# File 'string.c', line 7856

VALUE
rb_sym_to_s(VALUE sym)
{
    ID id = SYM2ID(sym);

    return str_new3(rb_cString, rb_id2str(id));
}

#inspectString

Returns the representation of sym as a symbol literal.

:fred.inspect   #=> ":fred"

Returns:


7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
# File 'string.c', line 7814

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_symObject #internObject

In general, to_sym returns the Symbol corresponding to an object. As sym is already a symbol, self is returned in this case.


7875
7876
7877
7878
7879
# File 'string.c', line 7875

static VALUE
sym_to_sym(VALUE sym)
{
    return sym;
}

#lengthInteger

Same as sym.to_s.length.

Returns:


8014
8015
8016
8017
8018
# File 'string.c', line 8014

static VALUE
sym_length(VALUE sym)
{
    return rb_str_length(rb_id2str(SYM2ID(sym)));
}

#=~(obj) ⇒ Fixnum?

Returns sym.to_s =~ obj.

Returns:


7987
7988
7989
7990
7991
# File 'string.c', line 7987

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

#succObject

Same as sym.to_s.succ.intern.


7940
7941
7942
7943
7944
# File 'string.c', line 7940

static VALUE
sym_succ(VALUE sym)
{
    return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}

#lengthInteger

Same as sym.to_s.length.

Returns:


8014
8015
8016
8017
8018
# File 'string.c', line 8014

static VALUE
sym_length(VALUE sym)
{
    return rb_str_length(rb_id2str(SYM2ID(sym)));
}

#[](idx) ⇒ String #[](b, n) ⇒ String

Returns sym.to_s[].

Overloads:


8001
8002
8003
8004
8005
# File 'string.c', line 8001

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

#succObject

Same as sym.to_s.succ.intern.


7940
7941
7942
7943
7944
# File 'string.c', line 7940

static VALUE
sym_succ(VALUE sym)
{
    return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}

#swapcaseObject

Same as sym.to_s.swapcase.intern.


8079
8080
8081
8082
8083
# File 'string.c', line 8079

static VALUE
sym_swapcase(VALUE sym)
{
    return rb_str_intern(rb_str_swapcase(rb_id2str(SYM2ID(sym))));
}

#to_procObject

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

(1..3).collect(&:to_s)  #=> ["1", "2", "3"]

7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
# File 'string.c', line 7902

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;
    }
}

#id2nameString #to_sString

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"

Overloads:


7856
7857
7858
7859
7860
7861
7862
# File 'string.c', line 7856

VALUE
rb_sym_to_s(VALUE sym)
{
    ID id = SYM2ID(sym);

    return str_new3(rb_cString, rb_id2str(id));
}

#to_symObject #internObject

In general, to_sym returns the Symbol corresponding to an object. As sym is already a symbol, self is returned in this case.


7875
7876
7877
7878
7879
# File 'string.c', line 7875

static VALUE
sym_to_sym(VALUE sym)
{
    return sym;
}

#upcaseObject

Same as sym.to_s.upcase.intern.


8040
8041
8042
8043
8044
# File 'string.c', line 8040

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