module MyMod::MyOtherMod question

G

Gene Angelo

Can someone please explain what the difference is?

A assumed these were synonymous, but they are not:

module A
module B
...
end
end

Not equivalent to?:

module A::B
...
end

Thanks alot.
 
G

Gene Angelo

Gene said:
assumed these were synonymous, but they are not:


I stand corrected. It appears that in order to use the following syntax:

module A::B
...
end

module A must be defined.
 
B

Brian Candler

Gene said:
Gene said:
assumed these were synonymous, but they are not:


I stand corrected. It appears that in order to use the following syntax:

module A::B
...
end

module A must be defined.


Yes. But even then,

module A; end
module A::B
...
end

is different to

module A
module B
...
end
end

in the way that constants are looked up.

NameError: uninitialized constant A::B::FOO
from (irb):3
from :0123
=> nil
 
A

adrfer

The constant that classes are assigned to when defined may be prefixed
by the names of existing modules using the scope operator :):). When
this happens, the leading scope operator places the class or module in
the top-level scope.

Here is the example:

CONST =3D 'outer'

module A
CONST =3D 'inner'
end

module A
class B
def self.get_const
CONST
end
end
end

p A::B.get_const # =3D> inner

Now, class B is inserted into module A's namespace, but it's not
interpreted in the context of A. As a result, the reference to CONST
resolves to the top-level constant, not A's version.

class A::B
def self.get_const
CONST
end
end

p A::B.get_const # =3D> outer

Gene said:
Gene said:
assumed these were synonymous, but they are not:

I stand corrected. It appears that in order to use the following syntax=
:
module A::B
=A0 =A0...
end
module A must be defined.

Yes. But even then,

module A; end
module A::B
=A0 ...
end

is different to

module A
=A0 module B
=A0 =A0 ...
=A0 end
end

in the way that constants are looked up.

NameError: uninitialized constant A::B::FOO
=A0 from (irb):3
=A0 from :0>> module A; module B
123
=3D> nil
 

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
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top