123456789_123456789_123456789_123456789_123456789_

Class: Symbol

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, ::Comparable
Inherits: Object
Defined in: string.c,
string.c,
symbol.c

Overview

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::Comparable - Included

#<

Compares two objects based on the receiver's #<=> method, returning true if it returns -1.

#<=

Compares two objects based on the receiver's #<=> method, returning true if it returns -1 or 0.

#==

Compares two objects based on the receiver's #<=> method, returning true if it returns 0.

#>

Compares two objects based on the receiver's #<=> method, returning true if it returns 1.

#>=

Compares two objects based on the receiver's #<=> method, returning true if it returns 0 or 1.

#between?

Returns false if obj #<=> min is less than zero or if anObject #<=> max is greater than zero, true otherwise.

#clamp

Returns min if obj #<=> min is less than zero, max if obj #<=> max is greater than zero and obj otherwise.

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

Instance Attribute Details

#empty?Boolean (readonly)

Returns that sym is :“” or not.

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.

#==

#===

#=~(obj) ⇒ Integer?

Returns sym.to_s =~ obj.

#[](idx) ⇒ String #[](b, n) ⇒ String #slice(idx) ⇒ String #slice(b, n) ⇒ String
Also known as: #slice

Returns sym.to_s[].

#capitalize([options]) ⇒ Symbol

Same as sym.to_s.capitalize.intern.

#casecmp(other) ⇒ 1, ...

Case-insensitive version of #<=>. Currently, case-insensitivity only works on characters A-Z/a-z, not all of Unicode. This is different from #casecmp?.

#casecmp?(other) ⇒ true, ...

Returns true if sym and other are equal after Unicode case folding, false if they are not equal, and nil if other is not a symbol.

#downcase([options]) ⇒ Symbol

Same as sym.to_s.downcase.intern.

#encodingEncoding

Returns the ::Encoding object that represents the encoding of sym.

#id2nameString #to_sString

Alias for #to_s.

#inspectString

Returns the representation of sym as a symbol literal.

:fred.inspect   #=> ":fred"

#to_symsym #internsym
Also known as: #to_sym

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

#lengthInteger #sizeInteger
Also known as: #size

Same as sym.to_s.length.

#match(obj) ⇒ MatchData?

Returns sym.to_s.match(obj).

#match?(obj) ⇒ Boolean

Returns sym.to_s.match?(obj).

#next Also known as: #succ

Same as sym.to_s.succ.intern.

#lengthInteger #sizeInteger

Alias for #length.

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

Alias for #[].

#next #succ

Alias for #next.

#swapcase([options]) ⇒ Symbol

Same as sym.to_s.swapcase.intern.

#to_procProc

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

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

#id2nameString #to_sString
Also known as: #id2name

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"
:ginger.to_s    #=> "ginger"

#to_symsym #internsym

Alias for #intern.

#upcase([options]) ⇒ Symbol

Same as sym.to_s.upcase.intern.