Create Instance In Class Method
Join the DZone community and get the full member experience.Join For Free
When you are inside a class method, the value of self refers to the object that holds the class structure (the instance of class Class). This means that self.new creates a new instance of the class.
class something def self.some_class_method a_new_instance_of_something = self.new # .. end endTo be honest, currently I don't use this. I came up with the idea because I wanted to inherit new singleton classes from a base singleton class and keep the one and only instance in the base class. It works, but the idea is a manifestation of schizophrenia in class design or - to say it shorter - complete nonsense. One singleton, one class! In my case, the correct design was to use ordinary classes which all contain the real singleton.
Opinions expressed by DZone contributors are their own.