views:

50

answers:

2

This does not seem to work:

class Test
  private

  define_method :private_method do 
    "uh!"
  end
end

puts Test.new.private_method
+4  A: 
Test.instance_eval { private :private_method }

Or, just run

private :private_method

from within the Test class.

Brian Campbell
that was easy, thank you! :D
knoopx
+3  A: 

Module#private takes an optional argument for the method name:

class Test
 private :private_method
end

The above is of course equivalent to

Test.private :private_method # doesn't work

Except that Module#private is private, so you have to use reflection to circumvent the access restrictions:

Test.send :private, :private_method

No eval necessary.

Jörg W Mittag