Last Updated:

What is a RUBY object?

RUBY object

In object-oriented programming, an object is a fundamental concept. An object is an entity that serves as a container for data and controls access to that data. Associated with an object is a set of attributes that, in essence, are simply variables that belong to the object. (In this book, we will use the usual term "variable" as applied to attributes without hesitation.) In addition, an object is associated with a set of functions that provide an interface to the object's functionality. These functions are called methods.


It is important to note that any object-oriented language provides an encapsulation mechanism. In a conventional sense, this means, firstly, that the attributes and methods of an object are associated with this object, and secondly, that the scope of attributes and methods is limited by default to the object itself (application of the principle of hiding information).

An object is considered an instance of an object class (usually referred to simply as a class). A class can be thought of as a drawing or sample, and an object as a thing made from that drawing. Also, a class is often called an abstract type, that is, a type more complex than an integer or string of characters.

Creating an object (a class instance) is called instantiating. Some languages have explicit constructors and destructors—functions that perform the actions necessary to initialize and destroy an object, respectively. Note in passing that in Ruby there is something that can be called a constructor, but there is no analogue of a destructor (thanks to the presence of a garbage collection mechanism).

Sometimes a situation arises when some data has a wide scope of view, not limited to a single object, and it is incorrect to place a copy of such an attribute in each instance of a class. Consider, for example, the MyDogs class and three objects of this class: fido, rover, and spot. Each dog may have attributes such as age and date of vaccination. Suppose, however, that you also want to keep the name of the owner of all dogs. You can, of course, put it in every object, but this is a waste of memory, besides distorting the meaning of the design.

It is clear that the "owner name" attribute does not belong to a single object, but to the class as a whole. Such attributes (the syntax for defining them varies from language to language) are called class attributes (or class variables).

There are many situations in which a class variable may be needed. Suppose, for example, that you want to know how many objects of a certain class have been created. You could set a class variable, initialize it to zero, and increment it by one as each object is created.


This variable is associated with a class, not with any particular object. In terms of scope, it is no different from any other attribute, but there is only one copy of it for the entire set of objects of this class.

To distinguish class attributes from ordinary attributes, the latter are often referred to as object attributes (or instance attributes). In this book, the word "attribute" refers to an instance attribute, unless it is explicitly stated that it is a class attribute.

Similarly, an object's methods serve to control access to its attributes and provide a well-defined interface for this purpose. But sometimes it is desirable or even necessary to define a method associated with the class itself. Not surprisingly, a class method controls access to class variables, while also performing actions that extend to the entire class rather than to any particular object. As with attributes, we will assume that a method belongs to an object unless explicitly stated otherwise.

It's worth noting that, in a sense, all methods are methods of a class. No need to think that by creating a hundred objects, we generated a hundred copies of the code of the methods! However, the scope limitation rules state that the method of each object operates only on the data of the object on whose behalf it is called. Thus, we have the illusion that the methods of the objectand are associated with the objects themselves.