Module.private_constant做什么?有没有办法只列出私有常量?

时间:2020-11-29 15:58:34

Starting in Ruby 1.9.3, we can create private constants:

从Ruby 1.9.3开始,我们可以创建私有常量:

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

关于它的作用是否有良好的文档?有没有办法获得类似于调用常量的私有常量的名称

2 个解决方案

#1


3  

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.

从Ruby 2.1开始,虽然Module#常量仅包含公共常量,但如果设置inherit = false,则还会获得私有常量。因此,如果您在常量中找到常量(false)但不在常量中找到常量(并且您不关心继承的常量),那么这可能是一种或多或少可靠的方式来判断它是否是私有的。

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.

更新:看起来这是Ruby中的一个错误,并且可能在将来的版本中消失。

#2


10  

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.

在Ruby 1.9.3之前,没有私有常量这样的东西。要获得所有常量的列表,您可以简单地使用常量。

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...

从1.9.3开始,添加了private_constant,但由于没有什么是私密的,你可以做......

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

#1


3  

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.

从Ruby 2.1开始,虽然Module#常量仅包含公共常量,但如果设置inherit = false,则还会获得私有常量。因此,如果您在常量中找到常量(false)但不在常量中找到常量(并且您不关心继承的常量),那么这可能是一种或多或少可靠的方式来判断它是否是私有的。

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.

更新:看起来这是Ruby中的一个错误,并且可能在将来的版本中消失。

#2


10  

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.

在Ruby 1.9.3之前,没有私有常量这样的东西。要获得所有常量的列表,您可以简单地使用常量。

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...

从1.9.3开始,添加了private_constant,但由于没有什么是私密的,你可以做......

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