include semantics broken?

B

Bob Sidebotham

This seems very odd to me:

module A
module B
end
end

module X
include A
module Y
B # => uninitialized constant X::Y::B (NameError
end
end

If I move the "include A" either up one line (before "module X"), or
down one line (after "module Y"), then it works as expected. Since
include is supposed to import all the constants from one module to the
other, then surely this should make the constants of A available to
anything defined within X?

My intent, in doing this, is to include A's semantics only where they
are relevant...

This may or may not be the way it's supposed to work--but for me, it
does violate POLS.

Puzzled,
Bob Sidebotham
 
B

Brian Schröder

Bob said:
This seems very odd to me:

module A
module B
end
end

module X
include A
module Y
B # => uninitialized constant X::Y::B (NameError
end
end

If I move the "include A" either up one line (before "module X"), or
down one line (after "module Y"), then it works as expected. Since
include is supposed to import all the constants from one module to the
other, then surely this should make the constants of A available to
anything defined within X?

My intent, in doing this, is to include A's semantics only where they
are relevant...

This may or may not be the way it's supposed to work--but for me, it
does violate POLS.

Puzzled,
Bob Sidebotham

You can tell ruby to start from the "root"-namespace by prefixing with a
::. See below:

module A
module B
end
end

module X
include A
module Y
::A::B # => uninitialized constant X::Y::B (NameError
end
end

Regards,

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top