Project

General

Profile

Feature #9941

Issue a warning when `module` or `class` keyword causes re-initialization of a constant that will become the module/class name, instead of creating/reopening the module/class

Added by alexeymuranov (Alexey Muranov) over 5 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:63152]

Description

I think that it would be appropriate to issue a warning similar to "warning: already initialized constant" when "module" or "class" keyword causes re-initialization of a constant. Here is an example of code that looks suspicious to me and IMO should issue such a warning:

module A
  module B
    C = 1
  end
end

module D
  include A
end

puts D.constants # => B
puts D::B        # => A::B
puts D::B::C     # => 1

module D::B
end

puts D.constants # => B
puts D::B        # => D::B
puts D::B::C     # => uninitialized constant D::B::C (NameError)

P.S. I think this behavior is related to the impossibility to re-open anonymous classes/modules and to the somewhat strange IMO scoping rules for constants :).

Also available in: Atom PDF