5

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?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Filipe Miguel Fonseca
  • 6,400
  • 1
  • 31
  • 26

3 Answers3

8

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
Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
5
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
Christopher Creutzig
  • 8,656
  • 35
  • 45
4

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.
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191