Search code examples
rubymethodsmetaprogrammingaccess-specifier

How do I dynamically define a method as private?


This does not seem to work:

class Test
  private

  define_method :private_method do 
    "uh!"
  end
end

puts Test.new.private_method

Solution

  • Test.instance_eval { private :private_method }
    

    Or, just run

    private :private_method
    

    from within the Test class.