Created
January 12, 2012 07:10
-
-
Save shugo/1599198 to your computer and use it in GitHub Desktop.
How to invoke Module#constants on Module itself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Module.constants returns constants accessible at the place where the | |
# method is called. | |
class A | |
X = 1 | |
p Module.constants.include?(:X) #=> true | |
end | |
# Module#constants returns constants defined in the receiver. However, | |
# you need a trick to invoke Module#constants on Module itself. | |
p Module.instance_method(:constants).bind(Module).call #=> [] | |
# Another way to invoke Module#constants on Module itself suggested by | |
# Marc-Andre Lafortune in [ruby-core:42091] | |
# It's available only in CRuby 1.9, and I doubt that Matz has accepted the | |
# feature because it has been introduced by nobu secretly. | |
p Module.constants(Module) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good point. So I guess the clearest demonstration would be:
And I think
Module.instance_method(:constants).bind(Module).call
is actually a totally different function, which is defined in variable.c:1775 (from ruby-1.9.3-p0)https://github.com/ruby/ruby/blob/v1_9_3_0/variable.c#L1775-1809 rb_mod_constants (Module#constants)
https://github.com/ruby/ruby/blob/v1_9_3_0/eval.c#L286-328 rb_mod_s_constants (Module.constants)
I guess
rb_mod_s_constants
is overridingrb_mod_constants
insideModule
,and which will also call
rb_mod_constants
whenargv > 0
.This is so confusing :o
Thanks for the discussion.