How do I dynamically define a method as private?

knoopx picture knoopx · Sep 23, 2010 · Viewed 8k times · Source

This does not seem to work:

class Test
  private

  define_method :private_method do 
    "uh!"
  end
end

puts Test.new.private_method

Answer

Brian Campbell picture Brian Campbell · Sep 23, 2010
Test.instance_eval { private :private_method }

Or, just run

private :private_method

from within the Test class.