在运行时将实例方法设为私有

发布于 2024-08-19 10:01:09 字数 455 浏览 9 评论 0原文

在另一个对象中注册该对象后,我需要将一些实例方法设为私有。

我不想冻结该对象,因为它必须保持可编辑状态,只是功能较少。我不想取消这些方法的定义,因为它们是在内部使用的。

我需要的是:

class MyClass

  def my_method
    puts "Hello"
  end

end

a = MyClass.new
b = MyClass.new

a.my_method                            #=> "Hello"
a.private_instance_method(:my_method)
a.my_method                            #=> NoMethodError
b.my_method                            #=> "Hello"

有什么想法吗?

I need to make some instance methods private after registering that object in another object.

I don't want to freeze the object because it must remain editable, only with less functionality. And I don't want to undef the methods since they are used internally.

What I need is something like:

class MyClass

  def my_method
    puts "Hello"
  end

end

a = MyClass.new
b = MyClass.new

a.my_method                            #=> "Hello"
a.private_instance_method(:my_method)
a.my_method                            #=> NoMethodError
b.my_method                            #=> "Hello"

Any ideas?

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

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

发布评论

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

评论(3

想挽留 2024-08-26 10:01:09

您可以随时在方法名称上调用方法 private 将其设为私有:

>> class A
>> def m
>> puts 'hello'
>> end
>> end
=> nil
>> a = A.new
=> #<A:0x527e90>
>> a.m
hello
=> nil
>> class A
>> private :m
>> end
=> A
>> a.m
NoMethodError: private method `m' called for #<A:0x527e90>
    from (irb):227
    from /usr/local/bin/irb19:12:in `<main>'

或者,从类外部调用:

A.send :private, :m

You can call method private on the method name anytime to make it private:

>> class A
>> def m
>> puts 'hello'
>> end
>> end
=> nil
>> a = A.new
=> #<A:0x527e90>
>> a.m
hello
=> nil
>> class A
>> private :m
>> end
=> A
>> a.m
NoMethodError: private method `m' called for #<A:0x527e90>
    from (irb):227
    from /usr/local/bin/irb19:12:in `<main>'

or, from outside the class:

A.send :private, :m
遗失的美好 2024-08-26 10:01:09
class A
  def test
    puts "test"
  end
  def test2
    test
  end
end

a = A.new

class << a
  private :test
end

a.test2 # works
a.test  # error: private method
class A
  def test
    puts "test"
  end
  def test2
    test
  end
end

a = A.new

class << a
  private :test
end

a.test2 # works
a.test  # error: private method
五里雾 2024-08-26 10:01:09

什么是公共的,什么是私有的是每个类。但每个对象都可以有自己的类:

class Foo

  private

  def private_except_to_bar
    puts "foo"
  end

end

class Bar

  def initialize(foo)
    @foo = foo.dup
    class << @foo
      public :private_except_to_bar
    end
    @foo.private_except_to_bar
  end

end

foo = Foo.new
Bar.new(foo)    # => "foo"

foo.private_except_to_bar
# => private method `private_except_to_bar' called for #<Foo:0xb7b7e550> (NoMethodError)

但是很糟糕。考虑这些替代方案:

  • 只需公开该方法。
  • 探索替代设计。

What's public and what's private is per class. But each object can have its own class:

class Foo

  private

  def private_except_to_bar
    puts "foo"
  end

end

class Bar

  def initialize(foo)
    @foo = foo.dup
    class << @foo
      public :private_except_to_bar
    end
    @foo.private_except_to_bar
  end

end

foo = Foo.new
Bar.new(foo)    # => "foo"

foo.private_except_to_bar
# => private method `private_except_to_bar' called for #<Foo:0xb7b7e550> (NoMethodError)

But yuck. Consider these alternatives:

  • Just make the method public.
  • Explore alternative designs.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文