Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何定义模块中的self.include,使其通过其包含/扩展模块传递到后续的包含/扩展模块_Ruby - Fatal编程技术网

Ruby 如何定义模块中的self.include,使其通过其包含/扩展模块传递到后续的包含/扩展模块

Ruby 如何定义模块中的self.include,使其通过其包含/扩展模块传递到后续的包含/扩展模块,ruby,Ruby,我有许多模块,当它们被包含时,我希望它们调用“包含”函数。。这一功能对所有人都是一样的。。因此,我希望能够在一个模块中包含此功能,并使所有这些子模块都包含此功能。。这是一个人为的例子,但我需要一些像 module Humanable def self.extended(klass) klass.instance_eval do define_method :included do |base| puts 'I was just included into

我有许多模块,当它们被包含时,我希望它们调用“包含”函数。。这一功能对所有人都是一样的。。因此,我希望能够在一个模块中包含此功能,并使所有这些子模块都包含此功能。。这是一个人为的例子,但我需要一些像

module Humanable
  def self.extended(klass)
    klass.instance_eval do
      define_method :included do |base|
        puts 'I was just included into the Person Class'
      end
    end
  end
end

module Personable
  extend Human
end

class Person
  include Personable
end

其中,当Person包含Personable.时,它将从Humanable模块中放入字符串。。这不起作用,但希望你能想到我该如何实现这一点?

答案似乎是:

module Humanable
  def self.extended(klass)
    klass.define_singleton_method :included do |base|
      puts 'hello'
    end
  end
end

module Personable
  extend Humanable # does **not** puts 'hello'
end

class Person
  include Personable # puts 'hello'
end

你在标题中说“包括它的所有子模块”,但在你的例子中你扩展了它。你是什么意思?我试图澄清。。原谅我的困惑我不知道该怎么说。。这个例子可能是最好的,我想让“人物内人”的include执行puts“我刚刚被包括”行。。。但是当人格延伸到人的时候,这句话就不会执行了。我找到了我需要的答案。。感谢您的帮助,如果您已经解决了问题,您应该在下面发布您的答案,您可以在48小时后接受,或者,如果您认为您的问题/答案与未来的访问者无关,您可以完全删除您的问题。你不应该简单地用答案编辑你的问题。我想我在48小时内无法回答它。。我不知道我可以,但就是不能接受。。我会的。。谢谢