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

家住魔仙堡 提交于 2019-12-08 16:49:30

问题


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


回答1:


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.




回答2:


There is no such thing as private constants until Ruby 1.9.3. To get a list of all the constants though, you can simply use constants.

module Mod
  CONST = "value"
end

Mod.constants #=> [:CONST]

From 1.9.3, private_constant was added, but as nothing is really private, you can do...

module Mod
  CONST = "value"
  private_constant :CONST
end

Mod.const_get(:CONST) #=> "value"

I don't think there is a way of getting a list of all the private constants but you can still test the presence of a particular name.

Mod.const_defined?(:CONST) #=> true


来源:https://stackoverflow.com/questions/12944616/what-does-module-private-constant-do-is-there-a-way-to-list-only-private-consta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!