I am trying to keep the number of instances of objects of this class inside the class that defines these objects.
First of all, I know the reflection of the code and ObjectSpace.each_object, but I would prefer not to use reflection and let the class itself "follow up".
I looked around and all the solutions found seemed to use @@ class_variables in the class definition, for example, the accepted answer to this question: How to get class instances in Ruby?
As I was still reading, I found that class variables in ruby ββcan behave very badly in some situations ... The biggest reason is this:
A class variable defined at the top level of the program inherits all classes. It behaves like a global variable.
source and more detailed information here: http://ruby.runpaint.org/variables#class
So, I tried to encode a class that stores the number of its created objects inside itself, using the class instance variable instead of the class variable, and it seems to work fine, but since I still learn about this "deep" language topics I would like to ask you whether I wrote the code correctly and / or makes sense:
class Pizza @orders = 0 def self.new @orders += 1 end def self.total_orders @orders end end new_pizza = Pizza.new
One of my doubts is that overriding the Pizza.new method will βremoveβ some important functionality of the original .new method? (What does the original. New method offer? How can I "verify" the method code using reflection?) What else am I doing wrong or completely wrong in my approach and why?
thanks
edit: forgot to add this important bit:
As a way to better limit the amount of things, I would like the Pizza class to be able to calculate only during the creation of the object and not have the setter method in its @instance class variable, which can be accessed at any time in the code (Pizza.count = 1000). That is why I tried to redefine the "new."
I think this is the hardest part, because of which I ask myself: is my approach in the right direction or should I just stop treating these language mechanisms so much and instead add some logic to myself so that the calculation only happens if if an object of class Pizza introduced ObjectSpace ..
I'm just looking for a more elegant, not overblown way to get this using language features.
In any case, help would be appreciated.
Thanks again.