Issue #5887 has been updated by fxn (Xavier Noria).


I think this description could be better.

On one hand I find a little surprising that the first sentence mentions constants accessible at some point, whereas Module.constants returns top-level constants even if called in a subclass of BasicObject.

On the other hand, in addition to the names of classes and modules in the global scope, RUBY_PLATFORM, RbConfig, etc. are also returned.

And, to be more exact, the method returns the name of the top-level *constants*, which may or may not coincide with the name of the classes or modules they store. See for example

    class C
    end

    D = C
    Object.instance_eval { remove_const(:C) }

    p ::Module.constants.sort # contains :D, not :C

I could volunteer a patch with an alternative if it sounds good.
----------------------------------------
Bug #5887: The documentation of Module.constants is incorrect
https://bugs.ruby-lang.org/issues/5887#change-28119

Author: shugo (Shugo Maeda)
Status: Closed
Priority: Normal
Assignee: shugo (Shugo Maeda)
Category: core
Target version: 
ruby -v: ruby 2.0.0dev (2012-01-12 trunk 34015) [i686-linux]


The documentation of Module.constants says "Returns an array of the names of all constants defined in the system. This list includes the names of all modules and classes."  However, Module.constants returns the names of the constants accessible at the place where the method is called:

  class A
    X = 1
    p Module.constants.include?(:X) #=> true
  end

Could someone fix the documentation?  I can't write a proper English documentation.

OT: I think Module.constants should be renamed in the future, because Module.constants is confusing with Module#constants.  Why the hell do I have to write the following tricky code to invoke Module#constants on Module itself?

  p Module.instance_method(:constants).bind(Module).call



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