Constant Resolution Confusion

P

Phrogz

I was surprised by the following this weekend:

module Foo
module Bar; end
class Whee1
include Bar
end
end
# All is well

class Foo::Whee2
include Bar
end
#=> uninitialized constant Foo::Whee2::Bar (NameError)

Is the answer simply "lexically scoped resolution"?

Is the above still the case with 1.9?
 
H

hemant kumar

I was surprised by the following this weekend:

module Foo
module Bar; end
class Whee1
include Bar
end
end
# All is well

class Foo::Whee2
include Bar
end
#=> uninitialized constant Foo::Whee2::Bar (NameError)

Is the answer simply "lexically scoped resolution"?

Is the above still the case with 1.9?

Yuck..
And yes "problem" persists with Ruby1.9.



--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org
 
M

MonkeeSage

I was surprised by the following this weekend:

module Foo
module Bar; end
class Whee1
include Bar
end
end
# All is well

class Foo::Whee2
include Bar
end
#=> uninitialized constant Foo::Whee2::Bar (NameError)

Is the answer simply "lexically scoped resolution"?

Is the above still the case with 1.9?

Maybe it's because (I assume) this is a paired-down example, but in
the example class Whee1 is in the scope of module Foo, and so is
module Bar, but module Bar isn't in the scope of class Foo::Whee2, so
you have to include it as qualified Foo::Bar. It doesn't look like
module Bar is lexically scoped to class Foo.

Regards,
Jordan
 
P

Phrogz

Maybe it's because (I assume) this is a paired-down example, but in
the example class Whee1 is in the scope of module Foo, and so is
module Bar, but module Bar isn't in the scope of class Foo::Whee2, so
you have to include it as qualified Foo::Bar. It doesn't look like
module Bar is lexically scoped to class Foo.

The actual situation was that I started writing code all nested as in
the first example. Everything was working, my unit tests were passing,
all was right with the world.

But I didn't like the nesting. The extra level of folding was getting
in the way, and the non-explicit nesting made my function pop up not
the way I wanted.

So I changed the code in a way that I thought was purely formatting; I
pulled the classes (and modules) out to the top level with an explicit
Foo:: prefix for each. And I was surprised when my code stopped
working.

It was simple enough to see the error, and not very painful to find
all the cases where I had included the module and also add the Foo::
prefix. But it surprised me. Despite likely reading about this issue
previously, I had thought constant resolution would not be affected by
that particular edit.

*shrug*
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top