I have this parent class:
class ListBase
@@list = []
def self.list
@@list
end
end
I'm trying to create two child classes like so:
class Child1 < ListBase
end
class Child2 < ListBase
end
I was under the impression that each of these child classes will have their own @@list
class variable. However, I get this:
Child1.list.push(1)
Child2.list.push(2)
Child1.list # => [1, 2]
Child2.list # => [1, 2]
which means the child classes share the @@list
from the parent class.
How can I create a separate class variable for each of the child classes without repeating?
As @CarySwoveland comments, in your use case, you should use a class instance variable:
class ListBase
@list = []
def self.list
@list
end
end
Not even sure why you thought of using a class variable for your use case.