Starting with Ruby 2.1, Module#constants only includes public constants, if you set inherit=false , you will also get private constants. Therefore, if you find a constant in constants(false) , but not in constants (and you do not need inherited constants), this can be a more or less reliable way of saying whether it is confidential.
class Module def private_constants constants(false) - constants end end module Foo X = 1 Y = 2 private_constant :Y end puts "Foo.constants = #{Foo.constants}" puts "Foo.constants(false) = #{Foo.constants(false)}" puts "Foo.private_constants = #{Foo.private_constants}"
This is undocumented, and I'm not sure if he intends, but empirically it works. I would support this with unit tests.
Update: It looks like this is a bug in Ruby and may disappear in a future version.
David moles
source share