On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Object
Object is the default root of all Ruby objects. Object inherits from BasicObject which allows creating alternate object hierarchies. Methods on Object are available to all classes 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.
When referencing constants in classes inheriting from Object you do not need to use the full namespace. For example, referencing File inside YourClass will find the top-level File class.
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).
What’s Here
First, what’s elsewhere. Class Object:
-
Inherits from class BasicObject.
-
Includes module Kernel.
Here, class Object provides methods for:
Querying
-
!~: Returnstrueifselfdoes not match the given object, otherwisefalse. -
<=>: Returns 0 ifselfand the given objectobjectare the same object, or ifself == object; otherwise returnsnil. -
===: Implements case equality, effectively the same as calling==. -
eql?: Implements hash equality, effectively the same as calling==. -
kind_of?(aliased asis_a?): Returns whether given argument is an ancestor of the singleton class ofself. -
instance_of?: Returns whetherselfis an instance of the given class. -
instance_variable_defined?: Returns whether the given instance variable is defined inself. -
method: Returns theMethodobject for the given method inself. -
methods: Returns an array of symbol names of public and protected methods inself. -
nil?: Returnsfalse. (Onlynilrespondstrueto methodnil?.) -
object_id: Returns an integer corresponding toselfthat is unique for the current process -
private_methods: Returns an array of the symbol names of the private methods inself. -
protected_methods: Returns an array of the symbol names of the protected methods inself. -
public_method: Returns theMethodobject for the given public method inself. -
public_methods: Returns an array of the symbol names of the public methods inself. -
respond_to?: Returns whetherselfresponds to the given method. -
singleton_class: Returns the singleton class ofself. -
singleton_method: Returns theMethodobject for the given singleton method inself. -
singleton_methods: Returns an array of the symbol names of the singleton methods inself. -
define_singleton_method: Defines a singleton method inselffor the given symbol method-name and block or proc. -
extend: Includes the given modules in the singleton class ofself. -
public_send: Calls the given public method inselfwith the given argument. -
send: Calls the given method inselfwith the given argument.
Instance Variables
-
instance_variable_get: Returns the value of the given instance variable inself, ornilif the instance variable is not set. -
instance_variable_set: Sets the value of the given instance variable inselfto the given object. -
instance_variables: Returns an array of the symbol names of the instance variables inself. -
remove_instance_variable: Removes the named instance variable fromself.
Other
-
clone: Returns a shallow copy ofself, including singleton class and frozen state. -
define_singleton_method: Defines a singleton method inselffor the given symbol method-name and block or proc. -
dup: Returns a shallow unfrozen copy ofself. -
enum_for(aliased asto_enum): Returns anEnumeratorforselfusing the using the given method, arguments, and block. -
extend: Includes the given modules in the singleton class ofself. -
freeze: Prevents further modifications toself. -
hash: Returns the integer hash value forself. -
inspect: Returns a human-readable string representation ofself. -
itself: Returnsself. -
method_missing: Method called when an undefined method is called onself. -
public_send: Calls the given public method inselfwith the given argument. -
send: Calls the given method inselfwith the given argument. -
to_s: Returns a string representation ofself.
Class methods 1
Instance methods 41
ARGV contains the command line arguments used to run ruby.
A library like OptionParser can be used to process command-line arguments.
An alias for OptionParser.
Holds the original stderr
Holds the original stdin
Holds the original stdout
The Binding of the top level scope
Ruby version changes 3
-
3.4
-
Object#singleton_methodnow returns methods in modules prepended to or included in the receiver’s singleton class. [Bug #20620]Exampleo = Object.new o.extend(Module.new{def a = 1}) o.singleton_method(:a).call #=> 1
-
-
3.3
-
Now
Proc#dupandProc#clonecall initialize_dup and initialize_clone hooks respectively. [Feature #19362]
-
-
3.2
-
Proc#dupreturns an instance of subclass. [Bug #17545]
-