From: "shugo (Shugo Maeda)" Date: 2012-03-29T00:58:15+09:00 Subject: [ruby-core:43799] [ruby-trunk - Bug #5887] The documentation of Module.constants is incorrect Issue #5887 has been updated by shugo (Shugo Maeda). shugo (Shugo Maeda) wrote: > > Actually, you can simply pass a parameter to `Module.constants` and the singleton method will call the instance method. The documentation should definitely reflect this too... > > Oh, I didn't know that. However, I doubt that Matz has accepted the feature. > > The optional arguments of Module.constants were introduced by nobu in > r11338, but the change of Module.constants was not described in the > commit log. > > * intern.h, object.c, variable.c (rb_mod_constants): added an optional > flag to search ancestors, which is defaulted to true, as well as > const_defined? and const_get. [ruby-dev:29989] > > It was not discussed in the thread starting from [ruby-dev:29989] either. > > Is it an official feature? What do you think of this feature, Matz? The following code illustrates the current behavior of Module.constants. module A C1 = 1 end class Module include A C2 = 2 end module B C3 = 3 # with no argument, get constants available here p Module.constants #=> [:C3, :Object, :Module, :Class, ... # with an argument, the same behavior as Module#constants p Module.constants(false) #=> [:C2] p Module.constants(true) #=> [:C2, :C1] end I don't think this code is readable. It's better to have an alias of Module#constants such as `defined_constants' to avoid the name conflict. ---------------------------------------- Bug #5887: The documentation of Module.constants is incorrect https://bugs.ruby-lang.org/issues/5887#change-25304 Author: shugo (Shugo Maeda) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) 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/