Example:
class Base
@@var = "base"
def self.assign_var(var)
@@var = var
end
def self.show_var
@@var
end
def initialize
p @@var
end
end
class A < Base
assign_var("a")
end
class B < Base
assign_var("b")
end
class C < Base
assign_var("c")
end
p A.show_var # "c"
p B.show_var # "c"
p C.show_var # "c"
a = A.new # "c"
b = B.new # "c"
c = C.new # "c"
How to make them to show their own value assigned in their class? like this:
p A.show_var # "a"
p B.show_var # "b"
p C.show_var # "c"
a = A.new # "a"
b = B.new # "b"
c = C.new # "c"
UPDATE
I need to access this var in the initializer.
class Base
@var = "base"
def self.assign_var(var)
@var = var
end
def self.show_var
@var
end
def initialize
p @var
end
end
class A < Base
assign_var("a")
end
class B < Base
assign_var("b")
end
class C < Base
assign_var("c")
end
p A.show_var # "a"
p B.show_var # "b"
p C.show_var # "c"
a = A.new # nil
b = B.new # nil
c = C.new # nil
If I use Vu's solution, it is not working... Any ideas?