如何在 Ruby 中创建私有类常量

发布于 2024-09-02 09:38:01 字数 252 浏览 6 评论 0原文

在 Ruby 中如何创建私有类常量? (即在班级内部可见但外部不可见)

class Person
  SECRET='xxx' # How to make class private??

  def show_secret
    puts "Secret: #{SECRET}"
  end
end

Person.new.show_secret
puts Person::SECRET # I'd like this to fail

In Ruby how does one create a private class constant?
(i.e one that is visible inside the class but not outside)

class Person
  SECRET='xxx' # How to make class private??

  def show_secret
    puts "Secret: #{SECRET}"
  end
end

Person.new.show_secret
puts Person::SECRET # I'd like this to fail

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暗恋未遂 2024-09-09 09:38:01

从 ruby​​ 1.9.3 开始,您拥有 Module#private_constant 方法,这似乎正是您想要的:

class Person
  SECRET='xxx'.freeze
  private_constant :SECRET

  def show_secret
    puts "Secret: #{SECRET}"
  end
end

Person.new.show_secret
# => "Secret: xxx"

puts Person::SECRET
# NameError: private constant Person::SECRET referenced

Starting on ruby 1.9.3, you have the Module#private_constant method, which seems to be exactly what you wanted:

class Person
  SECRET='xxx'.freeze
  private_constant :SECRET

  def show_secret
    puts "Secret: #{SECRET}"
  end
end

Person.new.show_secret
# => "Secret: xxx"

puts Person::SECRET
# NameError: private constant Person::SECRET referenced
稚气少女 2024-09-09 09:38:01

您还可以将常量更改为类方法:

def self.secret
  'xxx'
end

private_class_method :secret

这使得它可以在类的所有实例中访问,但不能在外部访问。

You can also change your constant into a class method:

def self.secret
  'xxx'
end

private_class_method :secret

This makes it accessible within all instances of the class, but not outside.

伪心 2024-09-09 09:38:01

您可以使用 @@class_variable 代替常量,它始终是私有的。

class Person
  @@secret='xxx' # How to make class private??

  def show_secret
    puts "Secret: #{@@secret}"
  end
end
Person.new.show_secret
puts Person::@@secret
# doesn't work
puts Person.class_variable_get(:@@secret)
# This does work, but there's always a way to circumvent privateness in ruby

当然,然后 ruby​​ 不会采取任何措施来强制 @@secret 的恒定性,但是 ruby​​ 几乎不会采取任何措施来强制强制恒定性,所以......

Instead of a constant you can use a @@class_variable, which is always private.

class Person
  @@secret='xxx' # How to make class private??

  def show_secret
    puts "Secret: #{@@secret}"
  end
end
Person.new.show_secret
puts Person::@@secret
# doesn't work
puts Person.class_variable_get(:@@secret)
# This does work, but there's always a way to circumvent privateness in ruby

Of course then ruby will do nothing to enforce the constantness of @@secret, but ruby does very little to enforce constantness to begin with, so...

呆橘 2024-09-09 09:38:01

嗯...

@@secret = 'xxx'.freeze

有点作品。

Well...

@@secret = 'xxx'.freeze

kind of works.

与风相奔跑 2024-09-09 09:38:01

您可以首先将其设为私有方法

You can just literally make it a private method in the first place ????????‍♂️

class Person
  def SECRET
    'xxx'
  end

  def show_secret
    puts SECRET
  end
end

Person::SECRET    # Error: No such constant
Person.SECRET     # Error: No such method
Person.new.SECRET # Error: Call private method
person.new.show_secret # prints "xxx"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文