Double colon vs module block

Hi!

I am new to ruby. I have a question concerning namespaces. Is there a
difference between using the double colon operator or a module block?
Example:

module Library
class Book
end
end

vs.

class Library::Book
end

Daniel M. wrote:

Is there a
difference between using the double colon operator or a module block?

No, there is not.

Daniel M. wrote:

vs.

class Library::Book
end

Yes, there is. The second form will give you an error if module Library
was never defined with ‘module’ keyward. To ensure that the second form
always works, you may do the following:

module Library
end

class Library::Book
end

This will work even if module Library was previously defined.

Gennady.

On Mar 6, 1:21 pm, Daniel M. [email protected] wrote:

vs.

class Library::Book
end

The later will give an error if Library isn’t yet defined (personally
I with is would just auto-instantiate a new module). Also the lookup
of constants resolves differently. For example:

module Library
SOMETHING = “whatever”
end

class Library::Book
SOMETHING #=> NameError: uninitialized constant
end

But I think this has been fixed in Ruby 1.9… Can someone confirm
that?

T.

On Fri, Mar 07, 2008 at 03:25:40AM +0900, Sebastian H. wrote:

Daniel M. wrote:

Is there a
difference between using the double colon operator or a module block?

No, there is not.

Yes, there is.

If module A is not defined, then the following will fail:

module A::B
#…
end

…whereas the following will simply define module A…

module A
module B
#…
end
end

–Greg

Gregory S. wrote:

[I wrote:]

No, there is not.

Yes, there is.

Ok, yes, there is. And actually there is another one:
If Library is a class

module Library
class Book
end
end

will give you an error, while class Library::Book will work without
problems.

HTH,
Sebastian

On Thu, Mar 6, 2008 at 7:51 PM, Daniel M.
[email protected] wrote:

That’s exactly the exception I got. Can you explain why this happens?

I can’t explain you why it happens, but it works if you add the module
before the constant:
module A
SOMETHING = “bla”
end

class A::B
puts A::SOMETHING
end

Trans schrieb:

end
of constants resolves differently. For example:
that?

T.

That’s exactly the exception I got. Can you explain why this happens?

On Thu, Mar 6, 2008 at 1:51 PM, Daniel M.
[email protected] wrote:

end

But I think this has been fixed in Ruby 1.9… Can someone confirm
that?

T.

That’s exactly the exception I got. Can you explain why this happens?

module Foo::Baz directly ‘opens’ the Baz scope without opening the Foo
scope (for the purposes of constant resolution).

This code here illustrates the difference:

contrived example… ACTIVATE

module A
A_CONSTANT = 1
module B
B_CONSTANT = 2
end
end

module A # open A’s scope
module B # open B’s scope under A
p B_CONSTANT
p A_CONSTANT
end
end

module A::B # open B’s scope without opening A
p B_CONSTANT # B constants are resolved
p A_CONSTANT # This throws a NameError unless A_CONSTANT is defined
in B or TOPLEVEL
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs