レシーバークラスを反映してコードを生成するミックスインがあります。これは、次の単純な例のように、クラス定義の最後でクラス メソッドを実行する必要があることを意味します。
module PrintMethods
module ClassMethods
def print_methods
puts instance_methods
end
end
def self.included(receiver)
receiver.extend ClassMethods
end
end
class Tester
include PrintMethods
def method_that_needs_to_print
end
print_methods
end
ミックスインにこれを自動的に実行してもらいたいのですが、方法が思いつきません。私の最初の考えは、ミックスインに追加receiver.print_methods
するself.included
ことでしたが、それを反映させたいメソッドがまだ宣言されていないため、機能しません。クラスの最後に電話include PrintMethods
することもできましたが、それは悪い形のように感じます.
print_methods
クラス定義の最後に呼び出す必要がないように、これを実現するためのトリックはありますか?