Though there are many articles out there regarding singletons in ruby, I am goin to explain my version of it. In the beging singletons baffled me a lot, but a little reading of hack book of ruby and experiments, helped me get over the edge. To start with, we all know ruby is pure object oriented language, everything is an instance of something.
Here we have defined a class A which is instance of Class, and shown the tree it follows. class A inherits from Object and Object includes module Kernel. Class inherits from Module following Object and the Object including module Kernel.
Any object is an instance of its class, A.new i.e an instance of class A. Now, an object encapsulates state and behaves accordingly as defined in the methods. All objects have their own set of instance variables. As defined in the ruby source code.
// Structure for a general object
struct RObject {
struct RBasic basic;
struct st_table *iv_tbl;
} ;
st_table is the hash table where this information is stored.
But if we notice, what about the behavior specification? what about the methods? where are they?
// Structure for a general class
struct RClass {
struct RBasic basic;
struct st_table *iv_tbl;
struct st_table *m_tbl;
VALUE super;
};
here the from the definition of class from ruby code(ruby.h) we find that instance methods are placed in the "Class" of which the current object is instance of.
So to say if I have a class Person, then all instance methods will be manged in Person class. This is no surprise as all objects are unique so at individual level they need to maintain their own state, but method definition is common to all objects so its best to be kept at a single place i.e its class.
Now if you think more you'll start getting into the quagmire. What about the class_methods of Person class.
Where will be they?
Think ..................... !!!!!!!!!!!!!!!!!!
Hmmm the Class, oh yeah thats what the ruby Class source code we see.
BUT................ in that case all the classes will have all the other classes methods, as all classes are instance
of Class.
We are in a snake pit.
To breathe it easy "Ruby Singletons" come into the picture. These have different names not a surprise as we have different names for GODS ;).
Every class has a hidden class called singleton class as this class belongs uniquely to one class only hence the name.
It can be extracted as follows.
Here we see that the return value is a "Class" which is attached to "Class A", this is the class where class methods reside. These singleton classes follow the same tree as the actual Class.
Lets see the theory in action. Here we are defining a class named A with a class method "pingC".
In above steps, we have first extracted the exclusive class i.e the singleton and by using "class_eval" added instance method "pingEXC" to the singleton class.
As is evident from the result above "class A" has got "pingEXC" as its class method.
Interestingly we see that exclusive class is having "pingEXC" as instance method while "class A" is having it as class method. To get a list of instance methods for an object we query its class for instance methods(instance_methods method on the class of object in question).
No comments:
Post a Comment