Double colon vs module block

D

Daniel Mendler

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
 
G

Gennady Bystritsky

Daniel said:
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

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.
 
T

Trans

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

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.
 
G

Gregory Seidman

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
 
S

Sebastian Hungerecker

Gregory said:
[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
 
D

Daniel Mendler

Trans said:
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.
That's exactly the exception I got. Can you explain why this happens?
 
T

Thomas Wieczorek

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
 
W

Wilson Bilkovich

Trans schrieb:


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
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top