Forum: Ruby Double colon vs module block

33def21bd696e6c290c9f5fba47930c8?d=identicon&s=25 Daniel Mendler (Guest)
on 2008-03-06 19:22
(Received via mailing list)
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
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-03-06 19:26
(Received via mailing list)
Daniel Mendler wrote:
> Is there a
> difference between using the double colon operator or a module block?

No, there is not.
397f61cc4458e13157b4facf72325e5f?d=identicon&s=25 Gennady Bystritsky (Guest)
on 2008-03-06 19:32
(Received via mailing list)
Daniel Mendler 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.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2008-03-06 19:34
(Received via mailing list)
On Mar 6, 1:21 pm, Daniel Mendler <dmend...@wurzelteiler.de> 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.
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2008-03-06 19:35
(Received via mailing list)
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
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-03-06 19:40
(Received via mailing list)
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
33def21bd696e6c290c9f5fba47930c8?d=identicon&s=25 Daniel Mendler (Guest)
on 2008-03-06 19:52
(Received via mailing list)
Trans schrieb:
>>   end
> of constants resolves differently. For example:
> that?
>
> T.
>
>
That's exactly the exception I got. Can you explain why this happens?
88de1beaaacae26b839f2e6aebf857b6?d=identicon&s=25 Thomas Wieczorek (Guest)
on 2008-03-06 20:06
(Received via mailing list)
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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2008-03-08 21:34
(Received via mailing list)
On Thu, Mar 6, 2008 at 1:51 PM, Daniel Mendler
<dmendler@wurzelteiler.de> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.