L
lcor1979
Hello,
I'm new in the Ruby World and I have a question.
Let's say I have a class with a class attribute (I call this class
Parent), this class attribute is used in instance methods of Parent.
Parent class has many subsclasses. Each subsclass should have it's own
value of the class attribute.
Example:
class Parent
@@test = nil
def Parent.create(value)
@@test=value
new
end
def print_test
puts @@test
end
end
class Child1 < Parent
end
class Child2 < Parent
end
c1 = Parent.create '1'
c1.print_test # => 1
c2 = Parent.create '2'
c2.print_test # => 2
c1.print_test # => 2
What I'll like, is that Child1 and Child2 have their own values of
@@test, but @@test should be initialized and used in Parent.
Also, @@test cannot be an instance variable because it is heavy to
initialize and will be common to all instances of Child1 or Child2.
Thank you
I'm new in the Ruby World and I have a question.
Let's say I have a class with a class attribute (I call this class
Parent), this class attribute is used in instance methods of Parent.
Parent class has many subsclasses. Each subsclass should have it's own
value of the class attribute.
Example:
class Parent
@@test = nil
def Parent.create(value)
@@test=value
new
end
def print_test
puts @@test
end
end
class Child1 < Parent
end
class Child2 < Parent
end
c1 = Parent.create '1'
c1.print_test # => 1
c2 = Parent.create '2'
c2.print_test # => 2
c1.print_test # => 2
What I'll like, is that Child1 and Child2 have their own values of
@@test, but @@test should be initialized and used in Parent.
Also, @@test cannot be an instance variable because it is heavy to
initialize and will be common to all instances of Child1 or Child2.
Thank you