Ruby on rails 类加载后的Ruby包含链模块

Ruby on rails 类加载后的Ruby包含链模块,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一种情况,我必须在加载类之后加载我的模块。下面是示例代码 require 'active_support/concern' module A extend ActiveSupport::Concern def method_a puts "Method_a" end end module B extend ActiveSupport::Concern def method_b puts "Method_b" end end class Ab i

我有一种情况,我必须在加载类之后加载我的模块。下面是示例代码

require 'active_support/concern'
module A
  extend ActiveSupport::Concern
  def method_a
    puts "Method_a"
  end
end

module B
  extend ActiveSupport::Concern
  def method_b
    puts "Method_b"
  end
end

class Ab
  include B
  def calltest
    method_a
    method_b
  end
end

B.send(:include, A)

Ab.new.method_a
Ab.new.method_b

在上面的例子中,当我调用
方法a
时,它会抛出一个错误。但如果将该行移到
Ab
类上方,则效果良好。我不想在类
Ab
中包含
A
模块。有人能帮我打电话而不改变密码的顺序吗

为什么要将
A
包含在
B
中,而不是包含在
Ab
中?这将有助于:

# B.send(:include, A)
Ab.include A
您的代码不起作用,因为执行了
include B
,并且基本上将
B
中的所有方法都添加到了基中。一个人可以在
B
中实现
self.include
回调,收集包含在其中的所有类/模块,并在对其特征类调用
include
时更新所有类/模块,但这看起来像是一种奇怪的过度使用


旁注:是公共的。

您可以在模块
B
中包含模块
A

module A
  def method_a
    puts "Method_a"
  end
end

module B
  include A

  def method_b
    puts "Method_b"
  end
end

class Ab
  include B

  def calltest
    method_a
    method_b
  end
end

Ab.new.calltest
# Method_a
# Method_b
#=> nil

无需在
包含的
中放入
包含的
。它也应该像一个“正常”的包含一样工作。它现在是公开的,但过去不是。