我很难理解为什么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 中最外层的Module 类?即使班级是的后代,为什么失踪?class << self
singleton_class.module_exec
module_function
Module
更新: 为了使我的问题更加明确,将上述代码包装在新的模块定义中确实有效。我不明白为什么需要这种包装。
> 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