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

ruby>>
class Foo
private
def self.bar
end
end

Foo.bar #=> 呼べる
<<--

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

ruby>>
class Foo
def self.bar
end

private_class_method :bar
end

Foo.bar # => NoMethodError: private method `bar' called for Foo:Class
<<--

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

posted by genki genki on Mon 5 May 2008 at 04:39 with 2 comments
lukesilvia lukesilvia 05/05 14:49
privateもクラスレベルで行わないとだめです
class Hoge
class << self
private
def hoge
1
end
end
end
瀧内元気 瀧内元気 05/05 15:04
ご指摘の通りです。

ruby>>
class Hoge
class << self
private
def hoge
1
end
end
end
<<--

ということですね。
`private_class_method`は、結果的には上記と同様のことをしてくれる感じになります。
Cレベルのソースは以下のような感じですね。

c>>
static VALUE
rb_mod_private_method(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE);
return obj;
}
<<--

既存のメソッドの可視性をprivateにしています。
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://casino-pinco.clients.site/ https://casino-pinco.clients.site/: Pinco Casino регистрация открыла доступ к крутому ... 04/14 21:54
https://moscowremontnik.ru https://moscowremontnik.ru: Слышал много положительных отзывов о Московском ... 04/14 19:08
https://mellstroy.at https://mellstroy.at: Hi there! This is my first visit to your blog! We ... 04/14 19:03
https://russ-remont.ru/ https://russ-remont.ru/: Очень толковая статья, сохраню себе. 04/14 18:45
https://russ-remont.ru/ https://russ-remont.ru/: Приятно видеть профессиональный подход к делу. 04/14 17:56
https://russ-remont.ru/ https://russ-remont.ru/: Ваши советы сэкономили мне кучу времени. 04/14 17:39
https://russ-remont.ru/ https://russ-remont.ru/: Русский Ремонт — отличный выбор для тех, кто ценит... 04/14 17:26
https://www.remontexpress.ru https://www.remontexpress.ru: Радует, что компания работает официально по догово... 04/14 17:20