[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Double colon vs module block

Daniel Mendler

3/6/2008 6:22:00 PM

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 Mendler


8 Answers

Sebastian Hungerecker

3/6/2008 6:26:00 PM

0

Daniel Mendler wrote:
> Is there a
> difference between using the double colon operator or a module block?

No, there is not.

--
NP: Tool - Part Of Me (live)
Jabber: sepp2k@jabber.org
ICQ: 205544826

Gennady Bystritsky

3/6/2008 6:31:00 PM

0

Daniel Mendler wrote:
> 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.

Trans

3/6/2008 6:33:00 PM

0



On Mar 6, 1:21 pm, Daniel Mendler <dmend...@wurzelteiler.de> wrote:
> 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.

Gregory Seidman

3/6/2008 6:34:00 PM

0

On Fri, Mar 07, 2008 at 03:25:40AM +0900, Sebastian Hungerecker wrote:
> Daniel Mendler 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


Sebastian Hungerecker

3/6/2008 6:40:00 PM

0

Gregory Seidman 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Daniel Mendler

3/6/2008 6:52:00 PM

0

Trans schrieb:
> On Mar 6, 1:21 pm, Daniel Mendler <dmend...@wurzelteiler.de> wrote:
>
>> 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.
>
>
That's exactly the exception I got. Can you explain why this happens?

--
Daniel


Thomas Wieczorek

3/6/2008 7:06:00 PM

0

On Thu, Mar 6, 2008 at 7:51 PM, Daniel Mendler <dmendler@wurzelteiler.de> 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

Wilson Bilkovich

3/8/2008 8:33:00 PM

0

On Thu, Mar 6, 2008 at 1:51 PM, Daniel Mendler <dmendler@wurzelteiler.de> wrote:
> Trans schrieb:
>
>
> > On Mar 6, 1:21 pm, Daniel Mendler <dmend...@wurzelteiler.de> wrote:
> >
> >> 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.
> >
> >
> 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