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:


812
813
814
815
816
817
818
# File 'symbol.c', line 812

VALUE
rb_sym_all_symbols(void)
{
    VALUE ary = rb_ary_new2(global_symbols.str_sym->num_entries);
    st_foreach(global_symbols.str_sym, 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.

Returns:

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

9186
9187
9188
9189
9190
9191
9192
9193
# File 'string.c', line 9186

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) ⇒ Fixnum? #match(obj) ⇒ Fixnum?

Returns sym.to_s =~ obj.

Overloads:


9220
9221
9222
9223
9224
# File 'string.c', line 9220

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[].

Overloads:


9236
9237
9238
9239
9240
# File 'string.c', line 9236

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

#capitalizeObject

Same as sym.to_s.capitalize.intern.


9302
9303
9304
9305
9306
# File 'string.c', line 9302

static VALUE
sym_capitalize(VALUE sym)
{
    return rb_str_intern(rb_str_capitalize(rb_sym2str(sym)));
}

#casecmp(other) ⇒ -1, ...

Case-insensitive version of Symbol#<=>.

Returns:

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

9203
9204
9205
9206
9207
9208
9209
9210
# File 'string.c', line 9203

static VALUE
sym_casecmp(VALUE sym, VALUE other)
{
    if (!SYMBOL_P(other)) {
	return Qnil;
    }
    return rb_str_casecmp(rb_sym2str(sym), rb_sym2str(other));
}

#downcaseObject

Same as sym.to_s.downcase.intern.


9289
9290
9291
9292
9293
# File 'string.c', line 9289

static VALUE
sym_downcase(VALUE sym)
{
    return rb_str_intern(rb_str_downcase(rb_sym2str(sym)));
}

#empty?Boolean

Returns that sym is :“” or not.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

9263
9264
9265
9266
9267
# File 'string.c', line 9263

static VALUE
sym_empty(VALUE sym)
{
    return rb_str_empty(rb_sym2str(sym));
}

#encodingEncoding

Returns the Encoding object that represents the encoding of sym.

Returns:


9328
9329
9330
9331
9332
# File 'string.c', line 9328

static VALUE
sym_encoding(VALUE sym)
{
    return rb_obj_encoding(rb_sym2str(sym));
}

#id2nameString #to_sString

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"

Overloads:


9107
9108
9109
9110
9111
# File 'string.c', line 9107

VALUE
rb_sym_to_s(VALUE sym)
{
    return str_new_shared(rb_cString, rb_sym2str(sym));
}

#inspectString

Returns the representation of sym as a symbol literal.

:fred.inspect   #=> ":fred"

Returns:


9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
# File 'string.c', line 9069

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_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.


9124
9125
9126
9127
9128
# File 'string.c', line 9124

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

#lengthInteger #sizeInteger

Same as sym.to_s.length.

Overloads:


9250
9251
9252
9253
9254
# File 'string.c', line 9250

static VALUE
sym_length(VALUE sym)
{
    return rb_str_length(rb_sym2str(sym));
}

#=~(obj) ⇒ Fixnum? #match(obj) ⇒ Fixnum?

Returns sym.to_s =~ obj.

Overloads:


9220
9221
9222
9223
9224
# File 'string.c', line 9220

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

#succObject

Same as sym.to_s.succ.intern.


9166
9167
9168
9169
9170
# File 'string.c', line 9166

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

#lengthInteger #sizeInteger

Same as sym.to_s.length.

Overloads:


9250
9251
9252
9253
9254
# File 'string.c', line 9250

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[].

Overloads:


9236
9237
9238
9239
9240
# File 'string.c', line 9236

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

#succObject

Same as sym.to_s.succ.intern.


9166
9167
9168
9169
9170
# File 'string.c', line 9166

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

#swapcaseObject

Same as sym.to_s.swapcase.intern.


9315
9316
9317
9318
9319
# File 'string.c', line 9315

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

#to_procObject

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

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

9152
9153
9154
9155
# File 'string.c', line 9152

VALUE
rb_sym_to_proc(VALUE sym)
{
}

#id2nameString #to_sString

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"

Overloads:


9107
9108
9109
9110
9111
# File 'string.c', line 9107

VALUE
rb_sym_to_s(VALUE sym)
{
    return str_new_shared(rb_cString, rb_sym2str(sym));
}

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


9124
9125
9126
9127
9128
# File 'string.c', line 9124

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

#upcaseObject

Same as sym.to_s.upcase.intern.


9276
9277
9278
9279
9280
# File 'string.c', line 9276

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