Bug #5887
The documentation of Module.constants is incorrect
| Status: | Assigned | Start date: | 01/12/2012 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | core | |||
| Target version: | - | |||
| ruby -v: | ruby 2.0.0dev (2012-01-12 trunk 34015) [i686-linux] |
Description
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
Associated revisions
* eval.c: Improve rdoc for Module.constants [issue #5887]
History
Updated by naruse (Yui NARUSE) 4 months ago
- Status changed from Open to Assigned
- Assignee set to drbrain (Eric Hodel)
Updated by marcandre (Marc-Andre Lafortune) 4 months ago
Shugo Maeda wrote:
> 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
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...
I'll try to improve the doc.
Updated by shugo (Shugo Maeda) 4 months ago
Hi,
2012/1/13 Marc-Andre Lafortune <ruby-core@marc-andre.ca>:
>> 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
>
> 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?
--
Shugo Maeda
Updated by marcandre (Marc-Andre Lafortune) 4 months ago
- Category changed from DOC to core
- Assignee changed from drbrain (Eric Hodel) to matz (Yukihiro Matsumoto)
Oh, ok. Let's wait for Matz to confirm this feature.
Updated by shugo (Shugo Maeda) about 1 month ago
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.