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
-
.all_symbols ⇒ Array
Returns an array of all the symbols currently in Ruby's symbol table.
Instance Attribute Summary
-
#empty? ⇒ Boolean
readonly
Returns that sym is :“” or not.
Instance Method Summary
-
#<=>(other_symbol) ⇒ 1, ...
Compares
symbol
withother_symbol
after calling #to_s on each of the symbols. - #==
- #===
-
#=~(obj) ⇒ Fixnum?
(also: #match)
Returns
sym.to_s =~ obj
. -
#[](idx) ⇒ String
(also: #slice)
Returns
sym.to_s[]
. -
#capitalize ⇒ Symbol
Same as
sym.to_s.capitalize.intern
. -
#casecmp(other) ⇒ 1, ...
Case-insensitive version of #<=>.
-
#downcase ⇒ Symbol
Same as
sym.to_s.downcase.intern
. -
#encoding ⇒ Encoding
Returns the ::Encoding object that represents the encoding of sym.
-
#id2name ⇒ String
Alias for #to_s.
-
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
-
#intern ⇒ sym
(also: #to_sym)
In general, #to_sym returns the
Symbol
corresponding to an object. -
#length ⇒ Integer
(also: #size)
Same as
sym.to_s.length
. -
#match(obj) ⇒ Fixnum?
Alias for #=~.
-
#next
(also: #succ)
Same as
sym.to_s.succ.intern
. -
#size ⇒ Integer
Alias for #length.
-
#slice(idx) ⇒ String
Alias for #[].
-
#succ
Alias for #next.
-
#swapcase ⇒ Symbol
Same as
sym.to_s.swapcase.intern
. -
#to_proc ⇒ Proc
Returns a Proc object which respond to the given method by sym.
-
#to_s ⇒ String
(also: #id2name)
Returns the name or string corresponding to sym.
-
#to_sym ⇒ sym
Alias for #intern.
-
#upcase ⇒ Symbol
Same as
sym.to_s.upcase.intern
.
::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? |
Class Method Details
.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, :$>]
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.
#==
#===
Also known as: #match
Returns sym.to_s =~ obj
.
Also known as: #slice
Returns sym.to_s[]
.
#capitalize ⇒ Symbol
Same as sym.to_s.capitalize.intern
.
#casecmp(other) ⇒ 1
, ...
Case-insensitive version of #<=>.
#downcase ⇒ Symbol
Same as sym.to_s.downcase.intern
.
#encoding ⇒ Encoding
Returns the ::Encoding object that represents the encoding of sym.
Alias for #to_s.
#inspect ⇒ String
Returns the representation of sym as a symbol literal.
:fred.inspect #=> ":fred"
#to_sym ⇒ sym
#intern ⇒ sym
Also known as: #to_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.
Also known as: #size
Same as sym.to_s.length
.
Alias for #=~.
#next Also known as: #succ
Same as sym.to_s.succ.intern
.
Alias for #length.
Alias for #[].
#next
#succ
Alias for #next.
#swapcase ⇒ Symbol
Same as sym.to_s.swapcase.intern
.
#to_proc ⇒ Proc
Returns a Proc object which respond to the given method by sym.
(1..3).collect(&:to_s) #=> ["1", "2", "3"]
Also known as: #id2name
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
:ginger.to_s #=> "ginger"
#to_sym ⇒ sym
#intern ⇒ sym
sym
#intern ⇒ sym
Alias for #intern.
#upcase ⇒ Symbol
Same as sym.to_s.upcase.intern
.