In Files
- string.c
Parent
Methods
Included Modules
Files
- grammar.en.rdoc
- test.ja.rdoc
- contributing.rdoc
- contributors.rdoc
- dtrace_probes.rdoc
- extension.ja.rdoc
- extension.rdoc
- globals.rdoc
- keywords.rdoc
- maintainers.rdoc
- marshal.rdoc
- regexp.rdoc
- security.rdoc
- standard_library.rdoc
- syntax.rdoc
- assignment.rdoc
- calling_methods.rdoc
- control_expressions.rdoc
- exceptions.rdoc
- literals.rdoc
- methods.rdoc
- miscellaneous.rdoc
- modules_and_classes.rdoc
- precedence.rdoc
- refinements.rdoc
- README.ja.rdoc
- README.rdoc
Class/Module Index
- ArgumentError
- Array
- BasicObject
- Bignum
- Binding
- Class
- ClosedQueueError
- Comparable
- Complex
- Complex::compatible
- ConditionVariable
- Continuation
- Data
- Dir
- ENV
- EOFError
- Encoding
- Encoding::CompatibilityError
- Encoding::Converter
- Encoding::ConverterNotFoundError
- Encoding::InvalidByteSequenceError
- Encoding::UndefinedConversionError
- EncodingError
- Enumerable
- Enumerator
- Enumerator::Generator
- Enumerator::Lazy
- Enumerator::Yielder
- Errno
- Exception
- FalseClass
- Fiber
- FiberError
- File
- File::Constants
- File::Stat
- FileTest
- Fixnum
- Float
- FloatDomainError
- GC
- GC::Profiler
- Hash
- IO
- IO::EAGAINWaitReadable
- IO::EAGAINWaitWritable
- IO::EINPROGRESSWaitReadable
- IO::EINPROGRESSWaitWritable
- IO::EWOULDBLOCKWaitReadable
- IO::EWOULDBLOCKWaitWritable
- IO::WaitReadable
- IO::WaitWritable
- IOError
- IndexError
- Integer
- Interrupt
- Kernel
- KeyError
- LoadError
- LocalJumpError
- Marshal
- MatchData
- Math
- Math::DomainError
- Method
- Module
- NameError
- NilClass
- NoMemoryError
- NoMethodError
- NotImplementedError
- Numeric
- Object
- ObjectSpace
- ObjectSpace::WeakMap
- Proc
- Process
- Process::GID
- Process::Status
- Process::Sys
- Process::UID
- Process::Waiter
- Queue
- Random
- Random::Formatter
- Range
- RangeError
- Rational
- Rational::compatible
- Regexp
- RegexpError
- RubyVM
- RubyVM::Env
- RubyVM::InstructionSequence
- RuntimeError
- ScriptError
- SecurityError
- Signal
- SignalException
- SizedQueue
- StandardError
- StopIteration
- String
- Struct
- Symbol
- SyntaxError
- SystemCallError
- SystemExit
- SystemStackError
- Thread
- Thread::Backtrace::Location
- Thread::Mutex
- ThreadError
- ThreadGroup
- Time
- TracePoint
- TrueClass
- TypeError
- UnboundMethod
- UncaughtThrowError
- ZeroDivisionError
- fatal
- unknown
Symbol
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
Public Class Methods
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.str_sym->num_entries); st_foreach(global_symbols.str_sym, symbols_i, ary); return ary; }
Public Instance Methods
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.
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)); }
Equality—If sym and obj are exactly the same symbol,
returns true
.
#define sym_equal rb_obj_equal
Equality—If sym and obj are exactly the same symbol,
returns true
.
#define sym_equal rb_obj_equal
Returns sym.to_s =~ obj
.
static VALUE sym_match(VALUE sym, VALUE other) { return rb_str_match(rb_sym2str(sym), other); }
Returns sym.to_s[]
.
static VALUE sym_aref(int argc, VALUE *argv, VALUE sym) { return rb_str_aref_m(argc, argv, rb_sym2str(sym)); }
Same as sym.to_s.capitalize.intern
.
static VALUE sym_capitalize(VALUE sym) { return rb_str_intern(rb_str_capitalize(rb_sym2str(sym))); }
Case-insensitive version of Symbol#<=>
.
static VALUE sym_casecmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return rb_str_casecmp(rb_sym2str(sym), rb_sym2str(other)); }
Same as sym.to_s.downcase.intern
.
static VALUE sym_downcase(VALUE sym) { return rb_str_intern(rb_str_downcase(rb_sym2str(sym))); }
Returns that sym is :“” or not.
static VALUE sym_empty(VALUE sym) { return rb_str_empty(rb_sym2str(sym)); }
Returns the Encoding object that represents the encoding of sym.
static VALUE sym_encoding(VALUE sym) { return rb_obj_encoding(rb_sym2str(sym)); }
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
VALUE rb_sym_to_s(VALUE sym) { return str_new_shared(rb_cString, rb_sym2str(sym)); }
Returns the representation of sym as a symbol literal.
:fred.inspect #=> ":fred"
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; }
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; }
Same as sym.to_s.length
.
static VALUE sym_length(VALUE sym) { return rb_str_length(rb_sym2str(sym)); }
Returns sym.to_s =~ obj
.
static VALUE sym_match(VALUE sym, VALUE other) { return rb_str_match(rb_sym2str(sym), other); }
Same as sym.to_s.succ.intern
.
static VALUE sym_succ(VALUE sym) { return rb_str_intern(rb_str_succ(rb_sym2str(sym))); }
Same as sym.to_s.length
.
static VALUE sym_length(VALUE sym) { return rb_str_length(rb_sym2str(sym)); }
Returns sym.to_s[]
.
static VALUE sym_aref(int argc, VALUE *argv, VALUE sym) { return rb_str_aref_m(argc, argv, rb_sym2str(sym)); }
Same as sym.to_s.succ.intern
.
static VALUE sym_succ(VALUE sym) { return rb_str_intern(rb_str_succ(rb_sym2str(sym))); }
Same as sym.to_s.swapcase.intern
.
static VALUE sym_swapcase(VALUE sym) { return rb_str_intern(rb_str_swapcase(rb_sym2str(sym))); }
Returns a Proc object which respond to the given method by sym.
(1..3).collect(&:to_s) #=> ["1", "2", "3"]
VALUE rb_sym_to_proc(VALUE sym) { }
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred"
VALUE rb_sym_to_s(VALUE sym) { return str_new_shared(rb_cString, rb_sym2str(sym)); }