ruby - Derived Class shares class variables? -
i have class cache , 2 derived classes foo , bar (simplification, principle same).
class cache @@test = [] def self.test @@test end def self.add(value) @@test << value end end class foo < cache end class bar < cache end running following leads me conclude @@test not unique foo , bar, unique cache, don't want nor expect.
foo.add(1) #[1] bar.add(2) #[1,2] puts foo.test #[1,2] isn't supposed be:
foo.add(1) #[1] bar.add(2) #[2] puts foo.test #[1] how can behaviour want? why ruby doing strange?
the solution in case not use static classes, 2 instances of class wanted , storing them in static variable.
Comments
Post a Comment