Ruby 语言 思想驱动生活

May 29, 2006

如何调用类的私有方法?

Filed under: Ruby — liubin @ 8:16
  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!

2 Comments

  1. download Friends with Money videos…

    DOWNLOAD MOVIE Friends with Money
    Just $2.99 for a complete movie! No additional software or browser plug-ins required! You can play them for unlimited number of times whenever you want. Downloaded movies will work perfectly on any PC, DVD player, PD…

    Trackback by the best movies review — September 18, 2008 @ 20:33

  2. full length Coyote Ugly vids…

    DOWNLOAD MOVIE Coyote Ugly
    Just $2.99 for a complete movie! No additional software or browser plug-ins required! You can play them for unlimited number of times whenever you want. Downloaded movies will work perfectly on any PC, DVD player, PDA etc.

    Trackback by review game film — September 20, 2008 @ 21:33

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.

Powered by WordPress