Skip to content

Instantly share code, notes, and snippets.

@godfat
Forked from shugo/constants.rb
Created January 12, 2012 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godfat/1599287 to your computer and use it in GitHub Desktop.
Save godfat/1599287 to your computer and use it in GitHub Desktop.
How to invoke Module#constants on Module itself
X = 1
# Module.constants returns constants accessible at the place where the
# method is called.
p Module.constants.include?(:X) #=> true
# 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 #=> []
p Module.instance_method(:constants).bind(Object).call.include?(:X) #=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment