Object

The superclass of all classes. Defines the general behavior of objects.

Included Modules

Methods

self == other

Checks whether self and other are equal. By default, has the same effect as equal?.

This method must be redefined with respect to each class's properties.

self === other

This method is mainly used for comparison within case statements. By default, acts like Object#==, but this behavior can be redefined in the subclass to implement an ownership check.

class

Returns the receiver class.

clone
dup

Creates a copy of the object. clone returns a complete clone of the original object, including freeze status and singleton methods, while dup duplicates the object contents only.

Note that clone and dup perform only a shallow copy. They duplicate the object itself, but nothing it points to (such as array elements).

For duplicated objects,

obj.equal?(obj.clone)

generally does not hold, while

obj == obj.clone

is correct in most cases.

Attempting to duplicate true, false, nil,Numeric objects, Symbol objects, and the like results in TypeError exceptions.

equal?(other)

When other is self, returns true. This method must not be redefined.

freeze

Prohibits an object's contents from being modified. Modifying a frozen object throws a TypeError exception.

frozen?

Returns true if the object's contents are prohibited from being modified.

inspect

Returns the object in a human-readable string format.

instance_of?(klass)

Returns true if self is a direct instance of the class klass. When obj.instance_of?(c) is true, obj.kind_of?(c) is always true as well.

instance_variable_get(var)

Gets and returns the value of the object's instance variables.

Specify the instance variable name for var using a character string or a Symbol.

Returns nil if the instance variable is undefined.

class Foo
  def initialize
    @foo = 1
  end
end

obj = Foo.new
p obj.instance_variable_get("@foo")     # => 1
p obj.instance_variable_get(:@foo)      # => 1
p obj.instance_variable_get(:@bar)      # => nil
instance_variable_set(var, val)

Sets object's instance variable to value val and returns val.

Specify the instance variable name for var using a character string or a Symbol.

A new instance variable is defined if one has not already been.

obj = Object.new
p obj.instance_variable_set("@foo", 1)  # => 1
p obj.instance_variable_set(:@foo, 2)   # => 2
p obj.instance_variable_get(:@foo)      # => 2
instance_variables

Returns the object's instance variable name as a string array.

obj = Object.new
obj.instance_eval { @foo, @bar = nil }
p obj.instance_variables

# => ["@foo", "@bar"]
is_a?(mod)
kind_of?(mod)

Returns true if self is the class mod (or its subclass) or a class (or its subclass) that includes the module mod.

module M
end
class C < Object
  include M
end
class S < C
end

obj = S.new
p obj.is_a? S       # true
p obj.is_a? M       # true
p obj.is_a? C       # true
p obj.is_a? Object  # true
p obj.is_a? Hash    # false
method(name)

Returns the Method object that encapsulated the name method of self in an object. name is specified with a Symbol or character string.

nil?

Returns true if the receiver is nil.

respond_to?(name[, priv=false])

Returns true if the object has the public method name.

name is a Symbol or character string. Returns true even for the private method if priv is true.

send(name[, args ... ])
send(name[, args ... ]) { .... }

Calls args to the argument, calls the object's name method, and returns the result of the method.

Passes the block as is when called with a block. The method name name is a character string or Symbol.

object_id

Returns a unique integer for each object. The integers are assigned to objects arbitrarily.

to_ary

Called internally when an object's implicit conversion to an array is required.

to_hash

Called internally when an object's implicit conversion to a hash is required.

to_int

Called internally when an object's implicit conversion to an integer is required.

to_s

Returns the string representation of the object.

When non-string objects are passed to arguments in print and sprintf, use this method to convert the objects to strings.

to_str

Called when an object's implicit conversion to a string is required.

Private Method

initialize

The object initialization method for user-defined classes. This method is called from Class#new to initialize a newly created object. The default behavior is to do nothing, as it is assumed that this method will be redefined in the subclass as needed. The argument given to Class#new will be passed to initialize as is.