0

Module#module_function次のirbセッションで が欠落している理由を理解するのに苦労しましたか?

> class << self      # open singleton for the session's object instance
>   p is_a? Module   # true
>   p respond_to? :module_function   # false ??
> end

私は何を達成しようとしていますか? irb セッションで外部モジュールを直接使用します。新しいモジュールにラップせずに。外部モジュールは、module_eval を使用してメソッドを動的に作成し、新しいメソッド名で module_function を呼び出します。

> require 'dlx/normalize'  # true
> class << self
>   extend DLX::Normalize   # main
>   generic_bind 'laplace-inverse'  # calling DLX::Normalize.generic_bind
> end
NoMethodError: undefined method `module_function' for #<Class:#<Object:0x0000000092e6e0>>

ここで何が欠けていますか? または正しい方法でirbの最も外側のモジュールクラスにアクセスしていますか? クラスの子孫であるにもかかわらず、 が欠落しているのはなぜですか?class << selfsingleton_class.module_execmodule_functionModule

更新: 私の質問をさらに明確にするために、上記のコードを新しいモジュール定義でラップすると機能します。なぜこのラッピングが必要なのか理解できません。

> require 'dlx/normalize'  # true
> module Dummy
>   extend DLX::Normalize   # main
>   generic_bind 'laplace-inverse'  # calling DLX::Normalize.generic_bind
>                                   # this method calls module_function
> end
> Dummy.generated_laplace(1.234)    # got new module's method
>                                   # previous call to module_function
>                                   # did succeeded
4

1 に答える 1