[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Constant Resolution Confusion

Phrogz

12/17/2007 6:08:00 PM

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?
3 Answers

hemant

12/17/2007 6:29:00 PM

0


On Tue, 2007-12-18 at 03:10 +0900, Phrogz wrote:
> 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://g...


MonkeeSage

12/17/2007 6:48:00 PM

0

On Dec 17, 12:07 pm, Phrogz <phr...@mac.com> wrote:
> 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

Phrogz

12/17/2007 7:01:00 PM

0

On Dec 17, 11:47 am, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 17, 12:07 pm, Phrogz <phr...@mac.com> wrote:
>
>
>
> > 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.

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*