Nested classes in RubyCalling shell commands from RubyAre static class variables possible?A concise explanation of nil v. empty v. blank in Ruby on RailsHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyCheck if a value exists in an array in Rubyclass << self idiom in RubyPython class inherits objectWhat is attr_accessor in Ruby?What does “Could not find or load main class” mean?

Possible runaway argument using circuitikz

Can you make an identity from this product?

How to safely destroy (a large quantity of) valid checks?

Separate SPI data

Did Apple bundle a specific monitor with the Apple II+ for schools?

Analogy between an unknown in an argument, and a contradiction in the principle of explosion

Who is "He that flies" in Lord of the Rings?

How do i export activities related to an account with a specific recordtype?

Is Lambda Calculus purely syntactic?

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

How can I remove material from this wood beam?

How long is it safe to leave marker on a Chessex battle map?

Electricity free spaceship

Difference between prepositions in "...killed during/in the war"

The usage of kelvin in formulas

Do you have to have figures when playing D&D?

A word that means "blending into a community too much"

2019 gold coins to share

Sql Server delete syntax

If I leave the US through an airport, do I have to return through the same airport?

C++ logging library

Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?

Can we completely replace inheritance using strategy pattern and dependency injection?

empApi with Lightning Web Components?



Nested classes in Ruby


Calling shell commands from RubyAre static class variables possible?A concise explanation of nil v. empty v. blank in Ruby on RailsHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyCheck if a value exists in an array in Rubyclass << self idiom in RubyPython class inherits objectWhat is attr_accessor in Ruby?What does “Could not find or load main class” mean?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I can't figure out how to initialize a class from a parent class variable. I'm trying to accomplish this:



x = A::B.new('my string')
myObject = x::C.new(**params)


and the program I'm calling is organized similar to:



module A
...<stuff>...
class B
...<stuff>....
class C < B
...<stuff>...
end
end
end


I want to initialize the class C after initializing the parent class B. That way, I can have access to class B's variables and methods from class C.



When I try to execute my program, I get:



"#<A::B:0x...............>" is not a class/module (TypeError)


Can anyone point me in the right direction for initializing nested classes like this?










share|improve this question
























  • It seems you expect this to work like Beta or Newspeak nested classes, but Ruby doesn't have nested classes. These are simply namespaced constants.

    – Jörg W Mittag
    Sep 4 '14 at 1:39

















1















I can't figure out how to initialize a class from a parent class variable. I'm trying to accomplish this:



x = A::B.new('my string')
myObject = x::C.new(**params)


and the program I'm calling is organized similar to:



module A
...<stuff>...
class B
...<stuff>....
class C < B
...<stuff>...
end
end
end


I want to initialize the class C after initializing the parent class B. That way, I can have access to class B's variables and methods from class C.



When I try to execute my program, I get:



"#<A::B:0x...............>" is not a class/module (TypeError)


Can anyone point me in the right direction for initializing nested classes like this?










share|improve this question
























  • It seems you expect this to work like Beta or Newspeak nested classes, but Ruby doesn't have nested classes. These are simply namespaced constants.

    – Jörg W Mittag
    Sep 4 '14 at 1:39













1












1








1








I can't figure out how to initialize a class from a parent class variable. I'm trying to accomplish this:



x = A::B.new('my string')
myObject = x::C.new(**params)


and the program I'm calling is organized similar to:



module A
...<stuff>...
class B
...<stuff>....
class C < B
...<stuff>...
end
end
end


I want to initialize the class C after initializing the parent class B. That way, I can have access to class B's variables and methods from class C.



When I try to execute my program, I get:



"#<A::B:0x...............>" is not a class/module (TypeError)


Can anyone point me in the right direction for initializing nested classes like this?










share|improve this question
















I can't figure out how to initialize a class from a parent class variable. I'm trying to accomplish this:



x = A::B.new('my string')
myObject = x::C.new(**params)


and the program I'm calling is organized similar to:



module A
...<stuff>...
class B
...<stuff>....
class C < B
...<stuff>...
end
end
end


I want to initialize the class C after initializing the parent class B. That way, I can have access to class B's variables and methods from class C.



When I try to execute my program, I get:



"#<A::B:0x...............>" is not a class/module (TypeError)


Can anyone point me in the right direction for initializing nested classes like this?







ruby class initialization nested






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 4 '14 at 0:26









sawa

134k31215311




134k31215311










asked Sep 4 '14 at 0:22









RitashugishaRitashugisha

4816




4816












  • It seems you expect this to work like Beta or Newspeak nested classes, but Ruby doesn't have nested classes. These are simply namespaced constants.

    – Jörg W Mittag
    Sep 4 '14 at 1:39

















  • It seems you expect this to work like Beta or Newspeak nested classes, but Ruby doesn't have nested classes. These are simply namespaced constants.

    – Jörg W Mittag
    Sep 4 '14 at 1:39
















It seems you expect this to work like Beta or Newspeak nested classes, but Ruby doesn't have nested classes. These are simply namespaced constants.

– Jörg W Mittag
Sep 4 '14 at 1:39





It seems you expect this to work like Beta or Newspeak nested classes, but Ruby doesn't have nested classes. These are simply namespaced constants.

– Jörg W Mittag
Sep 4 '14 at 1:39












3 Answers
3






active

oldest

votes


















2














You can't construct an instance from an instance - the whole module hierarchy is only applicable to modules and classes. Since x is neither, the parser has no idea what :: is supposed to do.



The idea behind nested classes is that they are namespaced within the parent class, no more, no less. In effect, class B acts like a module prefix to class C, but there's no inherent relationship between them (by being nested alone; you do inherit C from B in your example, which is what gives you the relationship), and certainly not between an instance of B and the class C



Instead, I would recommend constructing a C directly with 'my string', then calling #super in the initialize method, like so:



myObject = A::B::C.new('my string', **params)


and in the implementation:



module A
class B
def initialize(some_string)
@some_string = some_string
end
class C < B
def initialize(str, params)
super(str)
...
end
end
end
end





share|improve this answer
































    0














    As the error message suggests, the namespace operator :: can only be applied to a class/module. If x were the class A::B, then x::C should work. It does not make sense to apply ::C on an instance of A::B.






    share|improve this answer






























      0














      You need to apply the :: to x's class, not x itself. So you just need a call to the class method:



      x = A::B.new('my string')
      myObject = x.class::C.new(**params)


      For example, this sort of thing:



      module A
      class B
      class C
      end
      end
      end

      pancakes = A::B.new
      eggs = pancakes.class::C.new

      puts pancakes.inspect
      puts eggs.inspect


      gives you something like this:



      #<A::B:0x007faea90b6a58>
      #<A::B::C:0x007faea90b6a30>





      share|improve this answer

























      • This was extremely helpful to me! Thanks :D

        – Ritashugisha
        Sep 4 '14 at 4:03











      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',
      autoActivateHeartbeat: false,
      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%2f25655539%2fnested-classes-in-ruby%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can't construct an instance from an instance - the whole module hierarchy is only applicable to modules and classes. Since x is neither, the parser has no idea what :: is supposed to do.



      The idea behind nested classes is that they are namespaced within the parent class, no more, no less. In effect, class B acts like a module prefix to class C, but there's no inherent relationship between them (by being nested alone; you do inherit C from B in your example, which is what gives you the relationship), and certainly not between an instance of B and the class C



      Instead, I would recommend constructing a C directly with 'my string', then calling #super in the initialize method, like so:



      myObject = A::B::C.new('my string', **params)


      and in the implementation:



      module A
      class B
      def initialize(some_string)
      @some_string = some_string
      end
      class C < B
      def initialize(str, params)
      super(str)
      ...
      end
      end
      end
      end





      share|improve this answer





























        2














        You can't construct an instance from an instance - the whole module hierarchy is only applicable to modules and classes. Since x is neither, the parser has no idea what :: is supposed to do.



        The idea behind nested classes is that they are namespaced within the parent class, no more, no less. In effect, class B acts like a module prefix to class C, but there's no inherent relationship between them (by being nested alone; you do inherit C from B in your example, which is what gives you the relationship), and certainly not between an instance of B and the class C



        Instead, I would recommend constructing a C directly with 'my string', then calling #super in the initialize method, like so:



        myObject = A::B::C.new('my string', **params)


        and in the implementation:



        module A
        class B
        def initialize(some_string)
        @some_string = some_string
        end
        class C < B
        def initialize(str, params)
        super(str)
        ...
        end
        end
        end
        end





        share|improve this answer



























          2












          2








          2







          You can't construct an instance from an instance - the whole module hierarchy is only applicable to modules and classes. Since x is neither, the parser has no idea what :: is supposed to do.



          The idea behind nested classes is that they are namespaced within the parent class, no more, no less. In effect, class B acts like a module prefix to class C, but there's no inherent relationship between them (by being nested alone; you do inherit C from B in your example, which is what gives you the relationship), and certainly not between an instance of B and the class C



          Instead, I would recommend constructing a C directly with 'my string', then calling #super in the initialize method, like so:



          myObject = A::B::C.new('my string', **params)


          and in the implementation:



          module A
          class B
          def initialize(some_string)
          @some_string = some_string
          end
          class C < B
          def initialize(str, params)
          super(str)
          ...
          end
          end
          end
          end





          share|improve this answer















          You can't construct an instance from an instance - the whole module hierarchy is only applicable to modules and classes. Since x is neither, the parser has no idea what :: is supposed to do.



          The idea behind nested classes is that they are namespaced within the parent class, no more, no less. In effect, class B acts like a module prefix to class C, but there's no inherent relationship between them (by being nested alone; you do inherit C from B in your example, which is what gives you the relationship), and certainly not between an instance of B and the class C



          Instead, I would recommend constructing a C directly with 'my string', then calling #super in the initialize method, like so:



          myObject = A::B::C.new('my string', **params)


          and in the implementation:



          module A
          class B
          def initialize(some_string)
          @some_string = some_string
          end
          class C < B
          def initialize(str, params)
          super(str)
          ...
          end
          end
          end
          end






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 4 '14 at 0:35

























          answered Sep 4 '14 at 0:30









          MattMatt

          9,28412640




          9,28412640























              0














              As the error message suggests, the namespace operator :: can only be applied to a class/module. If x were the class A::B, then x::C should work. It does not make sense to apply ::C on an instance of A::B.






              share|improve this answer



























                0














                As the error message suggests, the namespace operator :: can only be applied to a class/module. If x were the class A::B, then x::C should work. It does not make sense to apply ::C on an instance of A::B.






                share|improve this answer

























                  0












                  0








                  0







                  As the error message suggests, the namespace operator :: can only be applied to a class/module. If x were the class A::B, then x::C should work. It does not make sense to apply ::C on an instance of A::B.






                  share|improve this answer













                  As the error message suggests, the namespace operator :: can only be applied to a class/module. If x were the class A::B, then x::C should work. It does not make sense to apply ::C on an instance of A::B.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 4 '14 at 0:29









                  sawasawa

                  134k31215311




                  134k31215311





















                      0














                      You need to apply the :: to x's class, not x itself. So you just need a call to the class method:



                      x = A::B.new('my string')
                      myObject = x.class::C.new(**params)


                      For example, this sort of thing:



                      module A
                      class B
                      class C
                      end
                      end
                      end

                      pancakes = A::B.new
                      eggs = pancakes.class::C.new

                      puts pancakes.inspect
                      puts eggs.inspect


                      gives you something like this:



                      #<A::B:0x007faea90b6a58>
                      #<A::B::C:0x007faea90b6a30>





                      share|improve this answer

























                      • This was extremely helpful to me! Thanks :D

                        – Ritashugisha
                        Sep 4 '14 at 4:03















                      0














                      You need to apply the :: to x's class, not x itself. So you just need a call to the class method:



                      x = A::B.new('my string')
                      myObject = x.class::C.new(**params)


                      For example, this sort of thing:



                      module A
                      class B
                      class C
                      end
                      end
                      end

                      pancakes = A::B.new
                      eggs = pancakes.class::C.new

                      puts pancakes.inspect
                      puts eggs.inspect


                      gives you something like this:



                      #<A::B:0x007faea90b6a58>
                      #<A::B::C:0x007faea90b6a30>





                      share|improve this answer

























                      • This was extremely helpful to me! Thanks :D

                        – Ritashugisha
                        Sep 4 '14 at 4:03













                      0












                      0








                      0







                      You need to apply the :: to x's class, not x itself. So you just need a call to the class method:



                      x = A::B.new('my string')
                      myObject = x.class::C.new(**params)


                      For example, this sort of thing:



                      module A
                      class B
                      class C
                      end
                      end
                      end

                      pancakes = A::B.new
                      eggs = pancakes.class::C.new

                      puts pancakes.inspect
                      puts eggs.inspect


                      gives you something like this:



                      #<A::B:0x007faea90b6a58>
                      #<A::B::C:0x007faea90b6a30>





                      share|improve this answer















                      You need to apply the :: to x's class, not x itself. So you just need a call to the class method:



                      x = A::B.new('my string')
                      myObject = x.class::C.new(**params)


                      For example, this sort of thing:



                      module A
                      class B
                      class C
                      end
                      end
                      end

                      pancakes = A::B.new
                      eggs = pancakes.class::C.new

                      puts pancakes.inspect
                      puts eggs.inspect


                      gives you something like this:



                      #<A::B:0x007faea90b6a58>
                      #<A::B::C:0x007faea90b6a30>






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 24 at 20:30

























                      answered Sep 4 '14 at 1:05









                      mu is too shortmu is too short

                      358k58707682




                      358k58707682












                      • This was extremely helpful to me! Thanks :D

                        – Ritashugisha
                        Sep 4 '14 at 4:03

















                      • This was extremely helpful to me! Thanks :D

                        – Ritashugisha
                        Sep 4 '14 at 4:03
















                      This was extremely helpful to me! Thanks :D

                      – Ritashugisha
                      Sep 4 '14 at 4:03





                      This was extremely helpful to me! Thanks :D

                      – Ritashugisha
                      Sep 4 '14 at 4:03

















                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25655539%2fnested-classes-in-ruby%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







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript