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 中最外层的Module 类?即使班级的后代,为什么失踪?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

0
Module.new.respond_to? :module_function
=> false

module Foo
  p respond_to? :module_function
end
=> false

所以你会在我期望的任何后代中得到相同的答案。

于 2013-03-30T21:27:34.043 回答