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;
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
add a comment |
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
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
add a comment |
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
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
ruby class initialization nested
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
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
.
add a comment |
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>
This was extremely helpful to me! Thanks :D
– Ritashugisha
Sep 4 '14 at 4:03
add a comment |
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
);
);
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%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
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
add a comment |
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
add a comment |
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
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
edited Sep 4 '14 at 0:35
answered Sep 4 '14 at 0:30
MattMatt
9,28412640
9,28412640
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Sep 4 '14 at 0:29
sawasawa
134k31215311
134k31215311
add a comment |
add a comment |
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>
This was extremely helpful to me! Thanks :D
– Ritashugisha
Sep 4 '14 at 4:03
add a comment |
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>
This was extremely helpful to me! Thanks :D
– Ritashugisha
Sep 4 '14 at 4:03
add a comment |
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>
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>
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
add a comment |
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
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.
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%2f25655539%2fnested-classes-in-ruby%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
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