Class

Object

Includes:

Object is the parent class of all classes in Ruby. Its methods are therefore available to all objects unless explicitly overridden.

Object mixes in the Kernel module, making the built-in kernel functions globally accessible. Although the instance methods of Object are defined by the Kernel module, we have chosen to document them here for clarity.

In the descriptions of Object’s methods, the parameter symbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).

Constants
ARGF
ARGV
DATA
ENV
ENV
FALSE
MatchingData
NIL
PLATFORM
RELEASE_DATE
RUBY_PATCHLEVEL
RUBY_PLATFORM
RUBY_RELEASE_DATE
RUBY_VERSION
STDERR
STDIN constants to hold original stdin/stdout/stderr
STDOUT
TOPLEVEL_BINDING
TRUE
VERSION obsolete constants
Public Methods
== Equality—At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.
=== Case Equality—For class Object, effectively the same as calling #==, but typically overridden by descendents to provide meaningful semantics in case statements.
=~ Pattern Match—Overridden by descendents (notably Regexp and String) to provide meaningful pattern-match semantics.
__id__ Document-method: object_id
__send__ Invokes the method identified by symbol, passing it any arguments specified. You can use __send__ if the name send clashes with an existing method in obj.
class Returns the class of obj, now preferred over Object#type, as an object’s type in Ruby is only loosely tied to that object’s class. This method must always be called with an explicit receiver, as class is also a reserved word in Ruby.
clone Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. Copies the frozen and tainted state of obj. See also the discussion under Object#dup.
display Prints obj on the given port (default $>). Equivalent to:
dup Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. dup copies the tainted state of obj. See also the discussion under Object#clone. In general, clone and dup may have different semantics in descendent classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendent object to create the new instance.
eql? Equality—At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.
equal? Equality—At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.
extend Adds to obj the instance methods from each module given as a parameter.
freeze Prevents further modifications to obj. A TypeError will be raised if modification is attempted. There is no way to unfreeze a frozen object. See also Object#frozen?.
frozen? Returns the freeze status of obj.
hash Generates a Fixnum hash value for this object. This function must have the property that a.eql?(b) implies a.hash == b.hash. The hash value is used by class Hash. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used.
id Soon-to-be deprecated version of Object#object_id.
inspect Returns a string containing a human-readable representation of obj. If not overridden, uses the to_s method to generate the string.
instance_eval Evaluates a string containing Ruby source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables. In the version of instance_eval that takes a String, the optional second and third parameters supply a filename and starting line number that are used when reporting compilation errors.
instance_of? Returns true if obj is an instance of the given class. See also Object#kind_of?.
instance_variable_defined? Returns true if the given instance variable is defined in obj.
instance_variable_get Returns the value of the given instance variable, or nil if the instance variable is not set. The @ part of the variable name should be included for regular instance variables. Throws a NameError exception if the supplied symbol is not valid as an instance variable name.
instance_variable_set Sets the instance variable names by symbol to object, thereby frustrating the efforts of the class’s author to attempt to provide proper encapsulation. The variable did not have to exist prior to this call.
instance_variables Returns an array of instance variable names for the receiver. Note that simply defining an accessor does not create the corresponding instance variable.
is_a? Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.
kind_of? Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.
method Looks up the named method as a receiver in obj, returning a Method object (or raising NameError). The Method object acts as a closure in obj’s object instance, so instance variables and the value of self remain available.
methods Returns a list of the names of methods publicly accessible in obj. This will include all the methods accessible in obj’s ancestors.
new Not documented
nil? call_seq:
object_id Returns an integer identifier for obj. The same number will be returned on all calls to id for a given object, and no two active objects will share an id. Object#object_id is a different concept from the :name notation, which returns the symbol id of name. Replaces the deprecated Object#id.
private_methods Returns the list of private methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.
protected_methods Returns the list of protected methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.
public_methods Returns the list of public methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.
remove_instance_variable Removes the named instance variable from obj, returning that variable’s value.
respond_to? Returns true> if obj responds to the given method. Private methods are included in the search only if the optional second parameter evaluates to true.
send Invokes the method identified by symbol, passing it any arguments specified. You can use __send__ if the name send clashes with an existing method in obj.
singleton_method_added Invoked as a callback whenever a singleton method is added to the receiver.
singleton_method_removed Invoked as a callback whenever a singleton method is removed from the receiver.
singleton_method_undefined Invoked as a callback whenever a singleton method is undefined in the receiver.
singleton_methods Returns an array of the names of singleton methods for obj. If the optional all parameter is true, the list will include methods in modules included in obj.
taint Marks obj as tainted—if the $SAFE level is set appropriately, many method calls which might alter the running programs environment will refuse to accept tainted strings.
tainted? Returns true if the object is tainted.
to_a Returns an array representation of obj. For objects of class Object and others that don’t explicitly override the method, the return value is an array containing self. However, this latter behavior will soon be obsolete.
to_s Returns a string representing obj. The default to_s prints the object’s class and an encoding of the object id. As a special case, the top-level object that is the initial execution context of Ruby programs returns ``main.’‘
type Deprecated synonym for Object#class.
untaint Removes the taint from obj.
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.