Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. p Module.constants.length # => 88
  2. $snapshot1 = Module.constants
  3. class A
  4. NAME=:abc
  5.  
  6. $snapshot2 = Module.constants
  7. p $snapshot2.length # => 90
  8. p $snapshot2 - $snapshot1 # => ["A", "NAME"]
  9.  
  10. end
  11. p Module.constants.length # => 89
  12. p Module.constants - $snapshot1 # => ["A"]
  13. p A.constants # => ["NAME"]
  14.  
  15. p A.class # => Class
  16. p A.class.ancestors # => [Class, Module, Object, Kernel]
  17.  
  18. # No argument: same class method as in 1.8:
  19. Module.constants # ==> All constants
  20. # One argument: uses the instance method:
  21. Module.constants(true) # ==> Constants of Module (and included modules)
  22. Module.constants(false) # ==> Constants of Module (only).
  23.  
  24. class << Module
  25. define_method :constants_of_module, Module.instance_method(:constants)
  26. end
  27.  
  28. # Now use this new class method:
  29. class Module
  30. COOL = 42
  31. end
  32. Module.constants.include?("COOL") # ==> false, as you mention
  33. Module.constants_of_module # ==> ["COOL"], the result you want
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement