1. class PT
  2.     private
  3.     def private_method
  4.         puts "private method executed!"
  5.     end
  6. end
  7.  
  8. p = PT.new
  9. begin
  10.   p.private_method
  11. rescue NoMethodError
  12.     puts "call Private method error!"
  13. end
  14.  
  15. PT.new.method(:private_method).call

最后执行结果如下:
>ruby C:/magic.rb
call Private method error!
private method executed!

Related posts for the current post: