title: Can a class in ruby store the number of instantiated objects using a @class_instance_variable and not a @@class_variable?
body: <p>I am trying to keep count of instances of objects of a given class, inside the class that defines those objects.</p>
  
  <p>First of all I am aware of code reflection and ObjectSpace.each_object, but I'd prefer not to use reflection and let the class itself be able to "look after" itself. </p>
  
  <p>I looked around and all solutions I found seem to make use of @@class_variables in the class definition like, for example, the accepted answer to this question: <a href="https://stackoverflow.com/questions/6365638/how-to-get-class-instances-in-ruby">How to get class instances in Ruby?</a></p>
  
  <p>As I kept reading around though, I found that class variables in ruby can behave very bad in some situations... biggest reason being this:</p>
  
  <blockquote>
    <p>A class variable defined at the top‐level of a program is inherited by
    all classes. It behaves like a global variable.</p>
  </blockquote>
  
  <p>source &amp; more info here: <a href="http://ruby.runpaint.org/variables#class" rel="nofollow noreferrer">http://ruby.runpaint.org/variables#class</a></p>
  
  <p>So, I tried to code a class that stores the number of its instantiated objects inside itself using a class instance variable instead of a class variable, and apparently it seems to work ok but since I am still learning about this "in-deep" language topics I'd like to ask you if the code I wrote is correct and/or makes sense:</p>
  
  <pre><code>class Pizza
    @orders = 0
    def self.new
      @orders += 1
    end
    def self.total_orders
      @orders
    end
  end
  
  new_pizza = Pizza.new #=&gt; 1
  special_pizza = Pizza.new #=&gt; 2
  fav_pizza = Pizza.new #=&gt; 3
  </code></pre>
  
  <p>One of my doubts is that overriding Pizza.new method i "delete" some important funcionality of the original .new method ? (What does the original .new method provide?? How can i "inspect" into a method code using reflection?) 
  What else am I doing wrong, or totally wrong in my approach and why ?</p>
  
  <p>Thanks</p>
  
  <p>edit: forgot to add this important bit: </p>
  
  <p>As a way to better constrain the scope of things, I'd like the Pizza class to be able to count only at object instantiation time and not to have a setter method on its @instance class variable, which can be accessed at anytime in code (Pizza.count = 1000). That's why I was trying to override "new". </p>
  
  <p>I think this is the trickiest part which makes me ask myself is my approach is in the right direction, or if I simply should stop reling so much on these language mechanisms and instead add myself some logic to let the counting happen only if a object of a Pizza class has entered ObjectSpace..</p>
  
  <p>I am just looking for the more elegant, non bloated way to obtain this using language features..</p>
  
  <p>In either case help would be appreciated..</p>
  
  <p>Thanks again.</p>