From: KURODA Hiraku Date: 2008-02-02T17:20:52+09:00 Subject: [ruby-list:44555] Re: include こんにちは、黒田ひらくと申します。 Masatoshi SEKI さんは書きました: > トップレベル(?)でincludeを使うと、どのオブジェクトも > モジュールに定義されているメソッドを呼べてしまう > のでしょうか? http://www.ruby-lang.org/ja/man/html/Ruby_CDD1B8ECBDB8.html#main によると、トップレベルのselfはObjectクラスのインスタンスですが、特異メソッドとしてinclude等が定義さ れているそうです。 このmain#includeを実行するとObject.includeと同様の効果があるので、その結果、全てのオブジェクトでイ ンクルードしたモジュールのメソットが呼べるようになるようです(全てのクラスはObjectクラスのサブクラス なので)。 ---ソース--- #!/usr/bin/ruby puts self, self.class module M def self.included(by) puts "M included by #{by}" end end include M puts Object.included_modules -------- ---出力---- main Object M included by Object M Kernel -------- -- -------------------------------------------------- 黒田 拓(くろだ ひらく) hiraku@sapporo.email.ne.jp http://www.hinet.mydns.jp/ --------------------------------------------------