Class variables are shared between all instances (therefore they are called class variables), so they will do what you want. They are also inherited, which sometimes leads to rather confusing behavior, but I don't think this will be a problem here. Here is an example of a class that uses a class variable to calculate how many instances of it have been created:
class Foo @@foos = 0 def initialize @@foos += 1 end def self.number_of_foos @@foos end end Foo.new Foo.new Foo.number_of_foos
sepp2k Mar 10 '10 at 11:24 2010-03-10 11:24
source share