Moduleクラスを拡張してごにょごにょ。
クラスを定義するとき使うpublic、alias_methodなどはModuleクラスのメソッドらしい。なので、定義機能を拡張できるとのこと。
よくわかってないのでとりあえず写経。
#!/usr/bin/env ruby class Module @@docs = { } # クラスの定義時に呼ばれる def doc(str) @@docs[self.name] = self.name + ":?n" + str.gsub(/^?s+/, '') end def Module::doc(aClass) # クラスに対する<=はクラスが同じまたはサブクラスかどうかチェック aClass = aClass.name if aClass.class <= Module @@docs[aClass] || "No documentation for #{aClass}" end end class Example doc "This is a sample documentation string" end module Another doc <<-edoc And this is a documentation string in a module edoc end puts Module::doc(Example) puts Module::doc("Another")
結果。
This is a sample documentation string Another: And this is a documentation string in a module
- 作者: Dave Thomas,Chad Fowler,Andy Hunt,まつもとゆきひろ,田和勝
- 出版社/メーカー: オーム社
- 発売日: 2006/08/26
- メディア: 大型本
- 購入: 7人 クリック: 270回
- この商品を含むブログ (152件) を見る