From: Wilson Bilkovich Date: 2008-03-09T05:33:22+09:00 Subject: Re: Double colon vs module block On Thu, Mar 6, 2008 at 1:51 PM, Daniel Mendler wrote: > Trans schrieb: > > > > On Mar 6, 1:21 pm, 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 > >> > > > > 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