Something about Ruby Classes

Tram Ho

The class Class

Class Class inherits directly from Module and includes all the behavior of the instance. In short, a Module is usually a repository for methods of one or more Classes , and Class is often used to manage the properties of an object.

Classes and ancestors

To see the superclass that Class inherits, we can use the superclass method:

Or see all classes that inherit in a sequential list, we use ancestors :

Classes are constants

In Ruby, when a new Class is defined, we are essentially creating an instance of the Class class:

In the example above we have just defined a class as Foo , which means that it is an instance of class Class and we can use this instance via constant Foo .

Following this logic, we can use another declaration, which is to create Foo like creating a normal object:

How does Ruby looks for a method?

When calling the instance method say_hello from the instance of class MyExample , ruby ​​will look in the class MyExample method with the same name, if any, it returns the match.

Similarly when calling the instance method say_good_bye , ruby ​​first finds the method with the corresponding name in the MyExample class, in case it is not found, it will go back to the ancestors class all the way up to the BasicObject class, if still not found, The returned result will be the NomethodError .

Include and Extend your Classes

To share methods in Module for Class, we can use include , extend , prepend methods. In this article we will focus on the first two methods only, as they are more popular than the other.


When a Class includes a Module, all instance methods of that Module become instance methods of the Class:

Like the above example we can see the ancestors class of RandomClass included RandomModule . When calling the say_thank_you method on the instance of RandomClass , ruby ​​will go to RandomClass to check the corresponding method, if not, it will continue to find the classes that RandomClass inherits, the first is RandomModule . Here the corresponding method is found and returns the desired result.


Although the general purpose is to use the same method between Class and Module, extend is completely different from include . If include used to add instance methods, then extend is responsible for adding class method:

After RandomModule to RandomClass , we can use the say_thank_you method as a class method. When we call the say_thank_you method from the instance of RandomClass , we get a NoMethodError result. But when calling directly from the RandomClass class, the result is completely consistent.

We can also use extend for only a particular class instance:

Classes are open

It is entirely possible to add or modify methods of ancestor classes, our own, or even Ruby’s built-in class:

In this example we have created a new instance of the tell_my_size method and overridden the available method reverse of Ruby’s String Class. Although this approach is quite useful in some cases, we should not overdo it and use extreme caution before adding or overriding Ruby’s existing methods.


The article is intended to share some interesting basic knowledge of Class in Ruby, the article is still flawed, thank you for your time.


Share the news now

Source : Viblo