Understanding the Eigenclass in less than 5 minutes
source link: https://www.tuicool.com/articles/hit/3I7vMfA
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Understanding the Eigenclass in less than 5 minutes
In this article, we’re going to explore the following topics:
- what’s the eigenclass ?
- the eigenclass behind the scene
Introduction
In Ruby, even a class is an object.
Indeed, a class is an instance of the class Class
.
Feel free to have a look to the Short guide to learning how Classes work in Ruby
article if you’re not familiar with the class Class
.
So, as a class is an object:
how a given class makes the difference between class-level and object-level methods, variables & constants ?
The answer is by using the eigenclass (a.k.a meta-class , shadow-class , singleton class , etc..).
Let’s detail what’s the purpose of this class and how it works behind the scene.
What’s the eigenclass ?
In Ruby, methods can be called by three ways:
MyClass.my_method my_object.my_method puts('puts is an implicit call to self.puts')
By default, when a method is defined in a class then this method becomes an instance method.
This instance method is accessible by all the instances (a.k.a objects) of this class
Here, we can see that the Greeting#hello
method is considered as an instance method.
This method is shared among all the instances of Greeting
.
Now let’s try to add a class method to our Greeting
class
Here the Greeting.hello
method is defined at a class level.
That’s why we can invoke this method using the Greeting
class as caller.
Also, we can see that the method is defined as a singleton method .
To keep it simple and — related to this article — let’s assume that singleton methods are class methods .
But what if I tell you that a singleton method is also an instance method ?
Well, this makes sense as (almost) everything in Ruby is an object.
What you commonly call a class method is, behind the scene, an instance method of the eigenclass .
By the way, the eigenclass is also referred as singleton class .
Ok.. But what exactly is the eigenclass ?
Let’s access to the eigenclass and inspect
what it contains in order to answer this question
First, we define a Greeting.eigenclass
method. This method returns self
in the context of the eigenclass — by using the class << self ... end
syntax.
Greeting.eigenclass
returns an instance of the class Class
(a.k.a an anonymous class).
This anonymous class keeps track of the class whose is attached to — the Greeting
class in our case.
Then, we can see that the singleton methods of the Greeting
class are the instance methods of the Greeting
eigenclass .
Note that defining our singleton method using self.hello
or Greeting.hello
produces the same result.
That’s cool. We now understand what’s the purpose of the eigenclass .
We also know that it’s an anonymous class.
So, now let’s have a deeper look at the properties of this anonymous class.
Theeigenclassbehind the scene
Let’s detail how the eigenclass is built behind the scene
As seen in the previous section, the eigenclass is an unnamed instance of the class Class
.
This anonymous class is defined within the class Greeting
.
We can ensure this by having a look at the return value of the Module.nesting
method.
This method returns the list of modules & classes nested at the point of call.
Then we can see that the eigenclass inherits from the Object
eigenclass —the #<Class:Object>
.
Indeed, as a class inherits from Object
by default, then it seems natural that a given eigenclass inherits from the Object
eigenclass by default.
Feel free to have a look at the Ruby Object Model
article if you’re not familiar with this ancestor chain and the Object
class in Ruby.
Conclusion
The eigenclass tends to be a pretty hard notion to apprehend.
But, in fact, it’s very well designed within the context of Ruby.
Indeed, when you know a bit about Ruby — especially about theRuby Object Model — the understanding of this notion becomes natural.
In another article I’ll cover more advanced topics about the eigenclass .
Finally, let’s try to give a proper definition of the eigenclass in Ruby:
The eigenclass is an unnamed instance of the class Class
attached to an object and which instance methods are used as singleton methods of the defined object.
Voilà!
ONE MORE THING ⬇
Feel free to subscribe here: www.rubycademy.com
Thank you for taking the time to read this post :-)
Feel free to :clap: and share this article if it has been useful for you. :rocket:
Also, as I post an article every 3 days, feel free to follow me to be notified when my articles are released.
Here is a link to my last article:
The Complete Guide to Create a Copy of an Object in Ruby: Part II
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK