Objects

From Ruby Standard Wiki

Jump to: navigation, search

6.1 General description

6.1: General description

Ruby is a pure object-oriented language. It is pure in the sense that every value manipulated in a Ruby program is an object including primitive values such as integers.

An object is a computational entity which has a state and a behavior.

A variable is a computational entity which stores a reference to an object. A variable has a name. A variable is said to be bound to an object if the variable stores a reference to the object. This association of a variable with an object is called a variable binding. When a variable with name N is bound to an object O, N is called the name of the binding, and O is called the value of the binding.

An object has a set of variable bindings. A variable whose binding is in this set is an instance variable of that object. This set of bindings of instance variables represents the state of that object and is encapsulated in that object.

A method is a procedure which, when invoked, performs a set of computations. The behavior of an object is defined by a set of methods which can be invoked on that object. A method has one or more (when aliased) names associated with it. An association between a name and a method is called a method binding. When a name N is bound to a method M, N is called the name of the binding, and M is called the value of the binding. A name to which a method is bound is called the method name. A method can be invoked on an object by specifying one of its names. The object on which the method is invoked is called the receiver of the method invocation.

There are three constructs which define the behavior of objects: classes, eigenclasses, and modules. A class defines methods shared by objects of the same class. An eigenclass is a special class which defines methods for only a single object. A module defines, and provides methods to be included into a class or another module. All these three constructs are represented as objects, which are dynamically created and modified at run-time.

A class creates objects, and the created objects are called direct instances of the class. A class defines a set of methods which can be invoked on all the instances of the class. These methods are instance methods of the class. A class is itself an object, and created by a class definition (see §13.2.2). A class has two sets of variable bindings besides a set of bindings of instance variables. The one is a set of bindings of constants. The other is a set of bindings of class variables, which represents the state shared by all the instances of the class.

Every object, including classes, can be associated with at most one special class to the object. This special class is called the eigenclass of the object. The eigenclass defines methods which can be invoked on that object. Those methods are singleton methods of the object. If the object is not a class, the singleton methods of the object can be invoked on only that object. If the object is a class, the singleton methods of the class are similar to so-called class methods because they can be invoked on only that class and its subclasses. An eigenclass is created, and associated with an object by an eigenclass definition (see §13.4.2) or a singleton method definition (see §13.4.3).

A class has a single class or nil as its direct superclass. If a class A has a class B as its direct superclass, A is called a direct subclass of B. Classes form a tree-like hierarchy defined by the direct superclass-subclass relation. There is only one class which has nil as its direct superclass. It is the root of the tree. All the ancestors of a class in the tree are called superclasses of the class. All the descendants of a class in the tree are called subclasses of the class. A class inherits constants, class variables, singleton methods, and instance methods from its superclasses, if any (see §13.2.3). If an object C is a direct instance of a class D, C is called an instance of D and all its superclasses.

Ruby does not support multiple inheritance; that is, a class can have only one direct superclass. However, Ruby supports module inclusion, which is a mechanism to append features into a class from multiple sources.

A module is an object which has the same structure as a class except that it cannot create an instance of itself and cannot be inherited. As with classes, a module has a set of class variables and instance methods. Instance methods and class variables defined in a module can be used by other classes, modules and eigenclasses by including the module into them. While a class can have only one direct superclass, a class or a module can include multiple modules. Instance methods defined in a module can be invoked on an instance of a class which includes the module. A module is created by a module definition (see §13.1.2).

Objects are created at some time during program execution. The lifetime of an object begins when the object is created and ends when all references to it are no longer possible.

6.2 Boolean values

6.2: Boolean values

An object is classified into either a true value or a false value.

Only false and nil are false values. The pseudo variable false is the only instance of the class FalseClass, and is represented by the keyword false. The pseudo variable nil is the only instance of the class NilClass, and represented by the keyword nil.

Objects other than false and nil are classified into true values. The pseudo variable true is the only instance of the class TrueClass, and represented by the keyword true.

Navigation

Previous: 5. Notational conventions Next: 7. Execution context

Personal tools