Class variables and inheritance

Philipp Meier wrote:

Hallo rubyers,

I’m a little puzzled with Class variables (@@foo) and inheritance:

class Super
@@foo = []

def self.manipulate(arg)
@@foo << arg
end

def get_foo
@@foo
end
end

class Sub1 < Super

manipulate “first”

end

class Sub2 < Super
manipulate "second"
end

Sub1.new.get_foo # => [“first”,“second”], wanted "first"
Sub2.new.get_foo # => [“first”,“second”], wanted “second”

How about this?

class A

def self.add(x)
(@foo||=[])<<x
end

def self.get
(@foo||=[])
end

def get
type.get
end
end

class A1 < A
add(“1”)
end

class A2 < A
add(“2”)
end

puts(A1.new.get.join(","))
puts(A2.new.get.join(","))

Best regards, Christian