Project

General

Profile

Actions

Feature #9941

open

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 10 years ago. Updated almost 3 years ago.

Status:
Open
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 :).

Actions #1

Updated by hsbt (Hiroshi SHIBATA) almost 3 years ago

  • Project changed from 14 to Ruby master
Actions

Also available in: Atom PDF

Like0
Like0