The class Class inherits without extend from Module, and adds to it the total behavior related to circumstances. Lengthy chronicle fast, Modules are inclined to add how to 1 or several Lessons of your app, and Lessons are inclined to managed your objects’ properties.
Ps: This diagram is terribly very very minimalist and focal level on Class, however Module and Object contain a great deal of varied children.
In show to peek the the ancestor of a Class, that you might perchance consume the .superclass attain :
And within the event that you might perchance treasure to peek the total superclasses of a specific class, that you might perchance consume the .ancestors attain :
=> [Class, Module, Object, Kernel, BasicObject]
Right here, the array involves the total superclasses of Class and here’s what we call the ancestor chain in ruby.
In Ruby, at the same time as you outline a fresh class, you are in fact creating an instance of the class Class.
To illustrate, here, we’ve upright created a fresh class named Foo, which is an instance of the class Class and we can entry to this instance by the utilization of the contant Foo.
Following this logic, we can see that we can contain created Foo treasure an casual object :
Foo = Class.fresh
locations 'howdy world'
=> [MyExample, Object, Kernel, BasicObject]>> MyExample.fresh.say_hello
=> howdy world>> MyExample.fresh.say_good_bye
say_hello : After we call the say_hello attain on an instance of the MyExample class, Ruby appears by the MyExample class for a attain named say_hello, it finds it and return the fitting result.
say_good_bye : If I call a say_good_bye attain, ruby will watch into MyExample for this attain, this might perchance well now not get it after which this might perchance well goes by the ancestor chain passing by every parent at a time except BasicObject to search out a attain called say_good_bye.
Because the say_good_bye attain doesn’t exist within the MyExample class or any of the ancestors, Ruby will return a NoMethodError.
Alongside with a Module’s code to a Class will be done the utilization of the consist of, lengthen and prepend programs. Let’s focal level on the two firsts as they are great extra commons.
As the doc says When a class involves a module, module’s instance programs change into available within the market as instance programs of the class.
consist of RandomModule
=> thanks>> RandomClass.ancestors
=> [RandomClass, RandomModule, Object, Kernel, BasicObject]
Right here that you might perchance additionally see that if we take a look at the ancestor chain of our RandomClass, the RandomModule appears to be like !
As we saw earlier, when we call the module’s attain (say_thank_you), Ruby will register our RandomClass class for the attain, this might perchance well now not get it so Ruby will battle by every of the ancestors of the RandomClass class in show to search out the attain.
I’ve always chanced on vague the adaptation between lengthen and consist of, however they are in fact very varied : lengthen adds the module’s attain as class programs and now not instance programs.
=> NoMethodError>> RandomClass.say_thank_you
=> thanks>> RandomClass.ancestors
=> [RandomClass, Object, Kernel, BasicObject]
As that you might perchance see, the module is mixed with our RandomClass, and it’s programs are now not accessible on the instance diploma (RandomClass.fresh.attain) however on the class diploma (RandomClass.attain).
You will be ready to even consume lengthen on one particular instance :
=> NoMethodError>> new_instance = RandomClass.fresh
>> new_instance.lengthen RandomModule
It attain that that you might perchance always add and edit programs of a specific ancestor class. You will be ready to place it for classes that you contain created your self or even from classes that are section of Ruby.
def reverse>> my_string = 'howdy world'
=> 11>> my_string.reverse
Now we contain got added a fresh attain to the String class and we’ve changed the behavior of the reverse attain.
Picture that it is clearly unhealthy to override varied Lessons this attain ! Be very careful by doing so and always pick to edit the existing classes without extend.
I do know there are many varied ideas round Lessons however that’s fervent in this article, in actuality feel free to enhance my explanation within the comments, I’d add extra ideas here at some point soon 👀
👉 I’am also the maker of stanza.dev that helps builders to be taught coding ideas faster. Be overjoyed to contain a behold 👨💻 👩💻