Metaprogramming Ruby: Core Concepts - Object

As the default root of all Rubric objects, Object holds quite a lot valuable instance methods for common usage. It mixes in the Kernel module to provide global accessibility of Kernel APIs for all inherited classes. This article will introduce Object definition.


Object only holds a few constants and instance methods for objects of descendants, all these methods are only used for instance level metaprogramming.


Gets class Constant of object.

define_singleton_method / new_method

Defines a singleton method in the receiver. The parameters can be a Proc, a Method or an UnboundMethod object. If a block is specified, it is used as the method body.


Enhance object with instance methods from modules given as a parameter.


Check if obj is an instance of the given class(Except ancestors).


Check if the given instance variable is defined in obj.


Gets the value of the given instance variable, or nil if the instance variable is not set. Just note that the @ part of the variable name should be included for regular instance variables.


Sets the instance variable named by symbol to the given object. The variable does not have to exist prior to this call.


Returns array of instance variables

is_a? / kind_of?

Returns true if class is the class of object, or if class is one of the superclasses of obj or modules included in obj.


returns object itself


Looks up the named method as a receiver in object, returning a Method object. The Method object acts as a closure in object’s object instance, so instance variables and the value of self remain available.


Returns a list of the names of public and protected methods of object. This will include all the methods accessible in obj’s ancestors. If the optional parameter is false, it returns an array of object’s public and protected singleton methods, the array will not include methods in modules included in object.


Returns the list of private methods accessible to object.


Returns the list of protected methods accessible to object.


Similar to method, searches public method only.


Returns the list of public methods accessible to obj.


Invokes the method identified by symbol, passing it any arguments specified. Unlike send, #public_send calls public methods only.


Removes the named instance variable from object, returning that variable’s value.


Hook method to return whether the object can respond to id method or not.

send / __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.


Returns the singleton class of object. This method creates a new singleton class if obj does not have one. If object is nil, true, or false, it returns NilClass, TrueClass, or FalseClass, respectively. If object is a Fixnum or a Symbol, it raises a TypeError.


Similar to #method, searches singleton method only.


Returns an array of the names of singleton methods for object. Only public and protected singleton methods are returned.