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?










share|improve this question
























  • 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

















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?










share|improve this question
























  • 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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.






share|improve this answer

















  • 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




















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]





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer

















    • 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

















    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.






    share|improve this answer

















    • 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















    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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
















    • 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














    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]





    share|improve this answer

























      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]





      share|improve this answer























        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]





        share|improve this answer












        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]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 6:21









        Fabio

        18.9k22044




        18.9k22044






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini