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 回答1: Test.instance_eval { private :private_method } Or, just run private :private_method from within the Test class. 回答2: It seems that starting with Ruby 2.1, define_method respects private : $ rvm 2.1.0 $ ruby /tmp/test.rb /tmp/test.rb:10:in `<main>': private method `private_method' called for #<Test:0x00000102014598> (NoMethodError) $ rvm 2.0 $ ruby /tmp/test.rb uh! (I