Class: Symbol

Inherits:
NSString show all

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 (collapse)

Instance Method Summary (collapse)

Methods inherited from NSString

availableStringEncodings, #boolValue, #cStringUsingEncoding:, #canBeConvertedToEncoding:, #capitalizedString, #capitalizedStringWithLocale:, #caseInsensitiveCompare:, #characterAtIndex:, #commonPrefixWithString:options:, #compare:, #compare:options:, #compare:options:range:, #compare:options:range:locale:, #completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:, #componentsSeparatedByCharactersInSet:, #componentsSeparatedByString:, #dataUsingEncoding:, #dataUsingEncoding:allowLossyConversion:, #decomposedStringWithCanonicalMapping, #decomposedStringWithCompatibilityMapping, defaultCStringEncoding, #doubleValue, #encode, #encode!, #enumerateLinesUsingBlock:, #enumerateLinguisticTagsInRange:scheme:options:orthography:usingBlock:, #enumerateSubstringsInRange:options:usingBlock:, #fastestEncoding, #fileSystemRepresentation, #floatValue, #getBytes:maxLength:usedLength:encoding:options:range:remainingRange:, #getCString:maxLength:encoding:, #getCharacters:range:, #getFileSystemRepresentation:maxLength:, #getLineStart:end:contentsEnd:forRange:, localizedNameOfStringEncoding:, localizedStringWithFormat:, pathWithComponents:, string, stringWithCString:encoding:, stringWithCharacters:length:, stringWithContentsOfFile:encoding:error:, stringWithContentsOfFile:usedEncoding:error:, stringWithContentsOfURL:encoding:error:, stringWithContentsOfURL:usedEncoding:error:, stringWithFormat:, stringWithString:, stringWithUTF8String:

Methods inherited from NSObject

#!, #!=, #!~, #===, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (Array) all_symbols

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:

Instance Method Details

- (-1, ...) <=>(other)

Compares sym with other in string form.

Returns:

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

- (Boolean) ==(obj)

Equality---If sym and obj are exactly the same symbol, returns true.

Returns:

  • (Boolean)

- (Fixnum?) =~(obj)

Returns sym.to_s =~ obj.

Returns:

- (String) [](idx) - (String) [](b, n)

Returns sym.to_s[].

Overloads:

- (Symbol) capitalize

Same as sym.to_s.capitalize.intern.

Returns:

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

Case-insensitive version of Symbol#.

Returns:

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

- (String) id2name - (String) to_s

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"

Overloads:

- (Symbol) downcase

Same as sym.to_s.downcase.intern.

Returns:

- (Boolean) empty?

Returns that sym is :"" or not.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (Boolean) ==(obj)

Equality---If sym and obj are exactly the same symbol, returns true.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (String) id2name - (String) to_s

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"

Overloads:

- (String) inspect

Returns the representation of sym as a symbol literal.

:fred.inspect   #=> ":fred"

Returns:

- (Object) to_sym - (Object) intern

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

- (Fixnum?) =~(obj)

Returns sym.to_s =~ obj.

Returns:

- (Object) succ

Same as sym.to_s.succ.intern.

- (Object) succ

Same as sym.to_s.succ.intern.

- (Symbol) swapcase

Same as sym.to_s.swapcase.intern.

Returns:

- (Object) to_proc

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

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

- (String) id2name - (String) to_s

Returns the name or string corresponding to sym.

:fred.id2name   #=> "fred"

Overloads:

- (Object) to_sym - (Object) intern

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

- (Symbol) upcase

Same as sym.to_s.upcase.intern.

Returns: