C++のノリで以下のように宣言しても、Rubyのクラスメソッドはprivateにはなりません。

   1  class Foo
   2  private
   3    def self.bar
   4    end
   5  end
   6  
   7  Foo.bar #=> 呼べる

Rubyでクラスメソッドをprivateにするためには、以下のように private_class_methodを利用します。

   1  class Foo
   2    def self.bar
   3    end
   4  
   5    private_class_method :bar
   6  end
   7  
   8  Foo.bar # => NoMethodError: private method `bar' called for Foo:Class

private_class_methodの宣言は、メソッド定義より後ろにある必要があります。

posted by Png genki on Mon 5 May 2008 at 04:44 with 2 comments
Contents
Rubyでクラスメソッドをprivateにする正しい方法
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ