GistTree.Com
Entertainment at it's peak. The news is by your side.

What I didn’t know about Ruby Classes

0

Olivier Dumas

I’ve always had this feeling that while finding out Ruby and Rails, there were hundreds microscopic notions that I didn’t in actuality understood. I’m inclined to jot down things in show to be taught them so here’s a assortment of fast articles about what I didn’t know.

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.

Image for post

Image for post

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 :

>> Class.superclass

=> Module

>> Module.superclass

=> Object

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.ancestors

=> [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.

class Foo

extinguish

>> Foo.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

>> Foo.class

=> Class
class MyExample   

def say_hello

locations 'howdy world'

extinguish

extinguish
>> MyExample.ancestors

=> [MyExample, Object, Kernel, BasicObject]
>> MyExample.fresh.say_hello

=> howdy world
>> MyExample.fresh.say_good_bye

=> NoMethodError

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.

Consist of

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.

module RandomModule

def say_thank_you

locations 'thanks'

extinguish

extinguish
class RandomClass

consist of RandomModule

extinguish
>> RandomClass.fresh.say_thank_you

=> 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.

Extend

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.

module RandomModule

def say_thank_you

locations 'thanks'

extinguish

extinguish
class RandomClass

lengthen RandomModule

extinguish
>> RandomClass.fresh.say_thank_you

=> 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 :

module RandomModule

def say_thank_you

locations 'thanks'

extinguish

extinguish
class RandomClass

extinguish
>> RandomClass.fresh.say_thank_you

=> NoMethodError
>> new_instance = RandomClass.fresh

>> new_instance.lengthen RandomModule

>> new_instance.say_thank_you

=> thanks

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.

class String

def tell_my_size

self.dimension

extinguish

def reverse

self.dimension

extinguish

extinguish

>> my_string = 'howdy world'

>> my_string.tell_my_size

=> 11
>> my_string.reverse

=> 11

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 👨‍💻 👩‍💻

Cheers ✌️

Other tales I’ve written ✍️

What I didn’t know about Ruby Numbers

What I didn’t know about MIME forms

Read More

Leave A Reply

Your email address will not be published.