Feature #6828


Constancy of Constants

Added by trans (Thomas Sawyer) over 10 years ago. Updated about 2 years ago.

Target version:


Could have swore there was a recent issue addressing this, but couldn't find it, so...

I hope by Ruby 2.0

module Foo::Bar::Baz

will be equivalent to

module Foo; module Bar; module Baz

That constant lookup doesn't work the same for both is really really really annoying.

Updated by alexeymuranov (Alexey Muranov) over 10 years ago

Did you mean #6810 by any chance?

Updated by MasterLambaster (Alex N) over 10 years ago

I think it might be implemented but i'd rather not do that.
The reason why its not equivalent is because the first statement means:
((|Define module Baz under Foo::Bar|))
The second one means:
((|Define module Foo, define module Bar under Foo, define module Baz under Foo::Bar|))

So it's not an issue in a common sense. In first case you try to create module under namespace that does not exist, that leads to error, and I think that's as it should be. In the second case you you explicitly define all module structure.


Updated by trans (Thomas Sawyer) over 10 years ago

It's not that. It's the constant lookup:

module Foo
X = 1
module Bar
p X #=> 1

module Foo::Bar
p X #=> NameError

Personally I would prefer undefined namespaces be automatically instantiated as modules. If that was not the intent another error will pick it up quickly enough anyway. But that's not the main issue --the constant lookup is the important thing.


Updated by trans (Thomas Sawyer) over 10 years ago

@alexeymuranov (Alexey Muranov) Yes, #6810 that's the one. Probably why I did not find it --it listed as a Bug. I would agree, actually.

Updated by drbrain (Eric Hodel) over 10 years ago

  • Target version changed from 2.0.0 to 3.0

I rely on this feature of constant lookup.

It allows me to define (({File})) and other top-level constants in my library's namespace, but still use the top-level (({File})) without littering (({::})) everywhere in my library. It also helps me reduce confusion of contributors. They don't have to wonder if "File" is (({::File})) or (({MyLibrary::File})), it's always the top-level constant unless listed under my namespace. (In particular, RDoc has (({RDoc::Encoding})) and would break with this change.)

Changing the constant lookup will break ruby libraries that use this feature, so I have changed the target to 3.0

Actions #6

Updated by naruse (Yui NARUSE) about 2 years ago

  • Target version deleted (3.0)

Also available in: Atom PDF