These public methods are in the Object
class, the base class for Ruby. This documentation is adapted and abbreviated from http://www.ruby-doc.org/core/classes/Object.html, where you can find code examples and longer explanations. Object
includes the Kernel
module, whose methods are listed in the next section.
obj
==
other
[or]
obj
.equal?(
other
)
[or]
obj
.eql?(
other
)
At the Object
level, ==
returns true
only if obj
and other
are the same object. Typically, this method is overridden in descendant classes to provide class-specific meaning. Unlike ==
, the equal?
method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b)
if and only if a
is the same object as b
). The eql?
method returns true
if obj
and other
have the same value.
obj
===
other
For class Object
, effectively the same as calling ==
, but typically overridden by descendants to provide meaningful semantics in case statements.
obj
=˜
other
Overridden by descendants (notably Regexp
and String
) to provide meaningful pattern-match semantics.
obj
._ _id_ _
[or]
obj
.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
.
obj
.class
Returns the class of obj
, now preferred over Object#type
, because 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, because class is also a reserved word in Ruby.
obj
.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
.
obj
.display(
port
=$>)
Prints obj
on the given port (default $>
).
obj
.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
.
obj
.equal?(
other
)
[or]
obj
.eql?(
other
)
[or]
obj
==
other
See ==
.
obj
.eql?(
other
)
[or]
obj
==
other
[or]
obj
.equal?(
other
)
See ==
.
obj
.extend(
module
,
. . . )
Adds to obj
the instance methods from each module
given as a parameter.
obj
.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?
.
obj
.frozen?
Returns the freeze status of obj
.
obj
.hash
Generates a Fixnum
hash value for this object.
obj
.id
Soon-to-be deprecated version of Object#object_id
.
obj
.inspect
Returns a string containing a human-readable representation of obj
. If not overridden, uses the to_s
method to generate the string.
obj
.instance_eval(
string
[,
filename
[,
lineno
]])
[or]
obj
.instance_eval { | |
block
}
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 lineno
that are used when reporting compilation errors.
obj
.instance_of?(class)
Returns true
if obj
is an instance of the given class. See also Object#kind_of?
.
obj
.instance_variable_defined?(symbol)
Returns true
if the given instance variable is defined in obj
.
obj
.instance_variable_get(symbol)
Returns the value of the given instance variable, or nil
if the instance variable is not set.
obj
.instance_variable_set(
symbol
,
obj
)
Sets the instance variable named 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.
obj
.instance_variables
Returns an array of instance variable names for the receiver.
obj
.is_a?(
class
)
[or]
obj.kind_of?(
class
)
Returns true
if class
is the class of obj
, or if class
is one of the superclasses of obj
or modules included in obj
.
obj
.method(sym)
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.
obj
.methods
Returns a list of the names of methods publicly accessible in obj
. This will include all the methods accessible in the ancestors of obj
.
obj
.nil?
[or]
nil.nil?
[or]
anything_else
.nil?
Returns true
if receiver is nil
. Only the object nil
responds true
to nil?
.
obj
.private_methods(
all
=true)
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.
obj
.protected_methods(
all
=true)
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.
obj
.public_methods(all=true)
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.
obj
.remove_instance_variable(symbol)
Removes the named instance variable from obj
, returning that variable's value.
obj
.respond_to?(
symbol
,
include_private
=false)
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
.
obj
.send(
symbol
[,
args
. . . ])
[or]
obj
._ _send_ _(
symbol
[,
args
. . . ])
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
.
obj.
singleton_method_added(
symbol
)
Invoked as a callback whenever a singleton method is added to the receiver.
obj.
singleton_method_removed(
symbol
)
Invoked as a callback whenever a singleton method is removed from the receiver.
obj.
singleton_method_undefined(
symbol
)
Invoked as a callback whenever a singleton method is undefined in the receiver.
obj
.singleton_methods(
all
=true)
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
.
obj
.taint
Marks obj
as tainted—if the $SAFE
level is set appropriately, many method calls which might alter the running program's environment will refuse to accept tainted strings.
obj
.tainted?
Returns true
if the object is tainted.
obj
.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.
obj
.to_enum(
method
=
:each
, *
args
)
[or]
obj
.enum_for(
method
=
:each
, *
args
)
Returns Enumerable::Enumerator.new(self, method, *args)
.
obj
.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
.
obj
.type
Deprecated synonym for Object#class
.
obj
.untaint
Removes the taint from obj
.