Create a new instance of parent's class variable for each child class
up vote
0
down vote
favorite
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?
ruby inheritance class-variables class-instance-variables
add a comment |
up vote
0
down vote
favorite
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?
ruby inheritance class-variables class-instance-variables
Check: stackoverflow.com/questions/1251352/…
– Marcin Kołodziej
Nov 9 at 4:39
1
I'm curious why you want to do that. Class instance variables are typically used for what you describe, as their scope is confined to the class in which they are created.
– Cary Swoveland
Nov 9 at 5:30
Actually, your question "How can I create a separate class variable for each of the child classes without repeating?" is contradictory. A class variable is for sharing among the child classes, and you are trying not to share it.
– sawa
Nov 9 at 8:34
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
ruby inheritance class-variables class-instance-variables
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?
ruby inheritance class-variables class-instance-variables
ruby inheritance class-variables class-instance-variables
edited Nov 9 at 8:31
sawa
129k27193298
129k27193298
asked Nov 9 at 4:35
Ashish Acharya
2,5191622
2,5191622
Check: stackoverflow.com/questions/1251352/…
– Marcin Kołodziej
Nov 9 at 4:39
1
I'm curious why you want to do that. Class instance variables are typically used for what you describe, as their scope is confined to the class in which they are created.
– Cary Swoveland
Nov 9 at 5:30
Actually, your question "How can I create a separate class variable for each of the child classes without repeating?" is contradictory. A class variable is for sharing among the child classes, and you are trying not to share it.
– sawa
Nov 9 at 8:34
add a comment |
Check: stackoverflow.com/questions/1251352/…
– Marcin Kołodziej
Nov 9 at 4:39
1
I'm curious why you want to do that. Class instance variables are typically used for what you describe, as their scope is confined to the class in which they are created.
– Cary Swoveland
Nov 9 at 5:30
Actually, your question "How can I create a separate class variable for each of the child classes without repeating?" is contradictory. A class variable is for sharing among the child classes, and you are trying not to share it.
– sawa
Nov 9 at 8:34
Check: stackoverflow.com/questions/1251352/…
– Marcin Kołodziej
Nov 9 at 4:39
Check: stackoverflow.com/questions/1251352/…
– Marcin Kołodziej
Nov 9 at 4:39
1
1
I'm curious why you want to do that. Class instance variables are typically used for what you describe, as their scope is confined to the class in which they are created.
– Cary Swoveland
Nov 9 at 5:30
I'm curious why you want to do that. Class instance variables are typically used for what you describe, as their scope is confined to the class in which they are created.
– Cary Swoveland
Nov 9 at 5:30
Actually, your question "How can I create a separate class variable for each of the child classes without repeating?" is contradictory. A class variable is for sharing among the child classes, and you are trying not to share it.
– sawa
Nov 9 at 8:34
Actually, your question "How can I create a separate class variable for each of the child classes without repeating?" is contradictory. A class variable is for sharing among the child classes, and you are trying not to share it.
– sawa
Nov 9 at 8:34
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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.
1
Thanks, yes once you point it out, it became pretty clear that I actually ought to be using a class instance variable.
– Ashish Acharya
Nov 9 at 12:32
add a comment |
up vote
1
down vote
You are using class variable through class method (getter/reader).
So you can override that method in derived classes and use own class variable for every derived class.
Not fun, but that how class variables works, they are shared within a class and derived classes as well.
class ListBase
def self.list
@@base_list ||=
end
end
class ListOne
def self.list
@@one_list ||=
end
end
class ListTwo
def self.list
@@two_list ||=
end
end
ListOne.list << 1
ListTwo.list << 2
puts ListOne.list
# => [1]
puts ListTwo.list
# => [2]
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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.
1
Thanks, yes once you point it out, it became pretty clear that I actually ought to be using a class instance variable.
– Ashish Acharya
Nov 9 at 12:32
add a comment |
up vote
1
down vote
accepted
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.
1
Thanks, yes once you point it out, it became pretty clear that I actually ought to be using a class instance variable.
– Ashish Acharya
Nov 9 at 12:32
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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.
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.
answered Nov 9 at 8:29
sawa
129k27193298
129k27193298
1
Thanks, yes once you point it out, it became pretty clear that I actually ought to be using a class instance variable.
– Ashish Acharya
Nov 9 at 12:32
add a comment |
1
Thanks, yes once you point it out, it became pretty clear that I actually ought to be using a class instance variable.
– Ashish Acharya
Nov 9 at 12:32
1
1
Thanks, yes once you point it out, it became pretty clear that I actually ought to be using a class instance variable.
– Ashish Acharya
Nov 9 at 12:32
Thanks, yes once you point it out, it became pretty clear that I actually ought to be using a class instance variable.
– Ashish Acharya
Nov 9 at 12:32
add a comment |
up vote
1
down vote
You are using class variable through class method (getter/reader).
So you can override that method in derived classes and use own class variable for every derived class.
Not fun, but that how class variables works, they are shared within a class and derived classes as well.
class ListBase
def self.list
@@base_list ||=
end
end
class ListOne
def self.list
@@one_list ||=
end
end
class ListTwo
def self.list
@@two_list ||=
end
end
ListOne.list << 1
ListTwo.list << 2
puts ListOne.list
# => [1]
puts ListTwo.list
# => [2]
add a comment |
up vote
1
down vote
You are using class variable through class method (getter/reader).
So you can override that method in derived classes and use own class variable for every derived class.
Not fun, but that how class variables works, they are shared within a class and derived classes as well.
class ListBase
def self.list
@@base_list ||=
end
end
class ListOne
def self.list
@@one_list ||=
end
end
class ListTwo
def self.list
@@two_list ||=
end
end
ListOne.list << 1
ListTwo.list << 2
puts ListOne.list
# => [1]
puts ListTwo.list
# => [2]
add a comment |
up vote
1
down vote
up vote
1
down vote
You are using class variable through class method (getter/reader).
So you can override that method in derived classes and use own class variable for every derived class.
Not fun, but that how class variables works, they are shared within a class and derived classes as well.
class ListBase
def self.list
@@base_list ||=
end
end
class ListOne
def self.list
@@one_list ||=
end
end
class ListTwo
def self.list
@@two_list ||=
end
end
ListOne.list << 1
ListTwo.list << 2
puts ListOne.list
# => [1]
puts ListTwo.list
# => [2]
You are using class variable through class method (getter/reader).
So you can override that method in derived classes and use own class variable for every derived class.
Not fun, but that how class variables works, they are shared within a class and derived classes as well.
class ListBase
def self.list
@@base_list ||=
end
end
class ListOne
def self.list
@@one_list ||=
end
end
class ListTwo
def self.list
@@two_list ||=
end
end
ListOne.list << 1
ListTwo.list << 2
puts ListOne.list
# => [1]
puts ListTwo.list
# => [2]
answered Nov 9 at 6:21
Fabio
18.9k22044
18.9k22044
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219935%2fcreate-a-new-instance-of-parents-class-variable-for-each-child-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check: stackoverflow.com/questions/1251352/…
– Marcin Kołodziej
Nov 9 at 4:39
1
I'm curious why you want to do that. Class instance variables are typically used for what you describe, as their scope is confined to the class in which they are created.
– Cary Swoveland
Nov 9 at 5:30
Actually, your question "How can I create a separate class variable for each of the child classes without repeating?" is contradictory. A class variable is for sharing among the child classes, and you are trying not to share it.
– sawa
Nov 9 at 8:34