Issue #6810 has been updated by alexeymuranov (Alexey Muranov).


If i express the rule for constant lookup as i have understood it, it seems that the lookup follows the code indentation from right to left, and in particular in

module A
  module B; N=1; end
  module B::C
    puts N # => NameError: uninitialized constant A::B::C::N
  end
end

the constant N will not be found because it is neither in A::B::C, nor in A, nor in Object, and A::B is not searched, because (hehe) there are only 2 levels of indentation at `puts N`.

What would you say then about the following proposal: when looking up a constant,

1. first, follow the inheritance hierarchy,
2. then, split the module name on '::' and follow the name hierarchy (this is what does not happen now),
3. last, follow the code nesting lexical hierarchy.
----------------------------------------
Bug #6810: `module A::B; end` is not equivalent to `module A; module B; end; end` with respect to constant lookup (scope)
https://bugs.ruby-lang.org/issues/6810#change-28538

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]


=begin
Is this the expected behavior?  To me it is rather surprising:

 N = 0

 module A
   module B
     def self.f; N; end
   end
   N = 1
 end

 A::B.f # => 1

but

 N = 0

 module A; end

 module A::B
   def self.f; N; end
 end

 module A
   N = 1
 end

 A::B.f # => 0

Even more striking:

 module A
   module B
     def self.f; N; end
   end
 end

 N = 0

 A::B.f # => 0

 A::N = 1

 puts A::B.f # => 1

 A::B::N = 2

 A::B.f # => 2

but

 module A; end

 module A::B
   def self.f; N; end
 end

 N = 0

 A::B.f # => 0

 A::N = 1

 A::B.f # => 0

 A::B::N = 2

 A::B.f # => 2


=end


-- 
http://bugs.ruby-lang.org/