Search code examples
rubyconstantsprivateruby-1.9.3

What does Module.private_constant do? Is there a way to list only private constants?


Starting in Ruby 1.9.3, we can create private constants:

module M
  class C; end
  private_constant :C
end

Is there a good documentation about what this does? Is there a way to get the names of only private constants similar to calling constants


Solution

  • As of Ruby 2.1, while Module#constants includes only public constants, if you set inherit=false, you will get private constants as well. So if you find a constant in constants(false) but not in constants (and you don't care about inherited constants), that might be a more or less reliable way to tell if it's private.

    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}"
    
    # => Foo.constants = [:X]
    # => Foo.constants(false) = [:X, :Y]
    # => Foo.private_constants = [:Y]
    

    This is undocumented, and I'm not sure if it's intentional, but empirically it works. I would back it up with unit tests.


    Update: It looks like this is a bug in Ruby, and may disappear in a future version.