Modules as namespaces

A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

I'm sure I'm just missing some finer point of modules as namespaces. But can
someone explain to me why line 26 in http://pastie.org/1418890 gives an
error (uninitialized constant Bar::B::A), but line 25 doesn't? I thought the
two modules would have the same structure. Thanks!

Andrew
 
A

Anurag Priyam

I'm sure I'm just missing some finer point of modules as namespaces. But can
someone explain to me why line 26 in http://pastie.org/1418890 gives an
error (uninitialized constant Bar::B::A), but line 25 doesn't? I thought the
two modules would have the same structure. Thanks!

What you are missing is the way Ruby resolves the name of constants.
It first tries to find the constant in the "lexical scope" of the
reference - immediately enclosing module/class, then the next
enclosing module/class, and so on. By defining Bar::B as "class
Bar::B; end" and not "module Bar; class B; end; end" you are changing
the scope in which the constant A should be resolved. You can find out
the modules/classes that are searched by calling Module.nesting
method; see http://pastie.org/1418923

--
Anurag Priyam,
3rd Year Undergraduate,
Department of Mechanical Engineering,
IIT Kharagpur.
+91-9775550642
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

Ah, this makes a lot more sense now, thanks!
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top