How do I dynamically define a method as private?

后端 未结 3 877
刺人心
刺人心 2021-01-04 08:52

This does not seem to work:

class Test
  private

  define_method :private_method do 
    \"uh!\"
  end
end

puts Test.new.private_method
3条回答
  •  无人及你
    2021-01-04 09:41

    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 `
    ': private method `private_method' called for # (NoMethodError) $ rvm 2.0 $ ruby /tmp/test.rb uh!

    (I realize this is an old question, but I happened across it via Google.)

提交回复
热议问题