1

I can access all the local variables with variable name "local_variables".

I can access all the global variables with variable name "global_variables".

But how can I access all the constants in a similar way, which are defined with capitalized first letter?

Konstantin
  • 2,983
  • 3
  • 33
  • 55
  • 2
    https://stackoverflow.com/questions/48386101/get-value-of-all-constants-defined-in-a-module – Sara Fuerst Aug 24 '20 at 19:25
  • How can I refer to the main module, if I have no explicit module structure? – Konstantin Aug 24 '20 at 21:03
  • 2
    Have you tried guessing? You get the local variables by calling the method `Kernel#local_variables`. You get the global variables by calling the method `Kernel#global_variables`. You get the instance variables by calling the method `Object#instance_variables`. You get the class variables by calling the method `Module#class_variables`. You get the instance methods by calling the method `Module#instance_methods`. You get the singleton methods by calling the method `Object#singleton_methods`. What might a method that gets the constants be called? – Jörg W Mittag Aug 24 '20 at 21:05
  • I got it: Module.constants But it enlists a lot of other, predefined constants too, this is why I din't noticed my own constants in the long list. – Konstantin Aug 24 '20 at 21:08
  • 1
    Now the problem is: how can I select my own constants form the long list? What criteria should I use, to pick the constants from the long list, which are defined by me, and not predefined constants? – Konstantin Aug 24 '20 at 21:12
  • 1
    You could probably use const_source_location (https://blog.saeloun.com/2019/09/17/ruby-2-7-module-const-source-location.html) to filter constants that are defined in your app source and those which are not. Ruby 2.7+ only though. – Siim Liiser Aug 25 '20 at 11:28
  • Very nice, thanks you very much. – Konstantin Aug 26 '20 at 00:06

0 Answers0