From: brad smith Date: 2013-06-04T05:05:26+09:00 Subject: Re: Cannot access constants from included modules in a submodule --089e0160a3b820f6ce04de457cb7 Content-Type: text/plain; charset=ISO-8859-1 can you put the actual code in a gist or something to make it easier to read. Thanks, Brad On Mon, Jun 3, 2013 at 11:38 AM, Bram S. wrote: > Hi there, > > I am trying to include a bunch of constants into a module and all its > "submodules" (by this I mean modules that are defined within the scope > of the first module; is there a word for that?), but the constants only > seem accessible in the module that directly includes them, not in a > submodule. See this example: > > === > module FooModule > > Foo = :foo > > end > > module BarModule > > include FooModule > > Bar = :bar > > p ancestors # => [BarModule, FooModule] > p Bar # works > p Foo # works > > module SubBarModule > > p ancestors # => [BarModule::SubBarModule] > p Bar # works > p Foo # fails > > end > > end > === > > Now if the SubBarModule's ancestors do not include FooModule, I can > understand that it cannot access Foo, but on the other hand, > SubBarModule's ancestors do not include BarModule either, so why can it > acces Bar? > > Tested in Ruby 1.8.7, 1.9.3p0 and 2.0.0p195, just to be sure that the > semantics have not changed -- it seems to be they haven't. > > Regards, > > Bram Senders > bupknar@gmail.com > > -- > Posted via http://www.ruby-forum.com/. > > --089e0160a3b820f6ce04de457cb7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
can you put the actual code in a gist or something to make= it easier to read.

Thanks,
Brad


On = Mon, Jun 3, 2013 at 11:38 AM, Bram S. <lists@ruby-forum.com> wrote:
Hi there,

I am trying to include a bunch of constants into a module and all its
"submodules" (by this I mean modules that are defined within the = scope
of the first module; is there a word for that?), but the constants only
seem accessible in the module that directly includes them, not in a
submodule. =A0See this example:

=3D=3D=3D
module FooModule

=A0 Foo =3D :foo

end

module BarModule

=A0 include FooModule

=A0 Bar =3D :bar

=A0 p ancestors # =3D> [BarModule, FooModule]
=A0 p Bar # works
=A0 p Foo # works

=A0 module SubBarModule

=A0 =A0 p ancestors # =3D> [BarModule::SubBarModule]
=A0 =A0 p Bar # works
=A0 =A0 p Foo # fails

=A0 end

end
=3D=3D=3D

Now if the SubBarModule's ancestors do not include FooModule, I can
understand that it cannot access Foo, but on the other hand,
SubBarModule's ancestors do not include BarModule either, so why can it=
acces Bar?

Tested in Ruby 1.8.7, 1.9.3p0 and 2.0.0p195, just to be sure that the
semantics have not changed -- it seems to be they haven't.

Regards,

Bram Senders
bupknar@gmail.com

--
Posted via http://= www.ruby-forum.com/.


--089e0160a3b820f6ce04de457cb7--