如何导入另一个模块';将类方法放入我的ruby类/模块中?

如何导入另一个模块';将类方法放入我的ruby类/模块中?,ruby,Ruby,我知道我可以导入实例方法,但是否可以导入类方法,以及如何导入?一个常见的习惯用法是: module Bar # object model hook. It's called when module is included. # Use it to also bring class methods in by calling `extend`. def self.included base base.send :include, InstanceMethods bas

我知道我可以导入实例方法,但是否可以导入类方法,以及如何导入?

一个常见的习惯用法是:

module Bar
  # object model hook. It's called when module is included. 
  # Use it to also bring class methods in by calling `extend`.
  def self.included base
    base.send :include, InstanceMethods
    base.extend ClassMethods
  end

  module InstanceMethods
    def hello
      "hello from instance method"
    end
  end

  module ClassMethods
    def hello
      "hello from class method"
    end
  end
end

class Foo
  include Bar
end

Foo.hello # => "hello from class method"
Foo.new.hello # => "hello from instance method"
InstanceMethods模块是怎么回事? 当我需要模块在类中同时包含实例和类方法时,我使用两个子模块。通过这种方式,方法被整齐地分组,例如,可以在代码编辑器中轻松地折叠

它也感觉更“统一”:这两种方法都是从
self.included
hook注入的

无论如何,这是个人喜好的问题。此代码的工作方式完全相同:

module Bar
  def self.included base
    base.extend ClassMethods
  end

  def hello
    "hello from instance method"
  end

  module ClassMethods
    def hello
      "hello from class method"
    end
  end
end

一个常见的习语是:

module Bar
  # object model hook. It's called when module is included. 
  # Use it to also bring class methods in by calling `extend`.
  def self.included base
    base.send :include, InstanceMethods
    base.extend ClassMethods
  end

  module InstanceMethods
    def hello
      "hello from instance method"
    end
  end

  module ClassMethods
    def hello
      "hello from class method"
    end
  end
end

class Foo
  include Bar
end

Foo.hello # => "hello from class method"
Foo.new.hello # => "hello from instance method"
InstanceMethods模块是怎么回事? 当我需要模块在类中同时包含实例和类方法时,我使用两个子模块。通过这种方式,方法被整齐地分组,例如,可以在代码编辑器中轻松地折叠

它也感觉更“统一”:这两种方法都是从
self.included
hook注入的

无论如何,这是个人喜好的问题。此代码的工作方式完全相同:

module Bar
  def self.included base
    base.extend ClassMethods
  end

  def hello
    "hello from instance method"
  end

  module ClassMethods
    def hello
      "hello from class method"
    end
  end
end

简短的回答是:不,您不能使模块对象本身的方法(“模块的类”方法)位于另一个对象的继承链中@Sergio的答案是一种常见的解决方法(通过将“类”方法定义为另一个模块的一部分)

您可能会发现以下图表很有指导意义(单击可查看全尺寸或全尺寸):


(来源:)


注意:Ruby 1.9的这个图表还没有更新,在Ruby 1.9中,有一些附加的核心对象,例如,这些对象会稍微改变根流。

简单的回答是:不,您不能使模块对象本身的方法(“模块的类”方法)位于另一个对象的继承链中@Sergio的答案是一种常见的解决方法(通过将“类”方法定义为另一个模块的一部分)

您可能会发现以下图表很有指导意义(单击可查看全尺寸或全尺寸):


(来源:)


注意:Ruby 1.9的这个图表还没有更新,在Ruby 1.9中有其他的核心对象,这些对象稍微改变了根流。

导入到底是什么意思?您有任何示例代码吗?
包括MyFancyModule
作为示例方法<代码>为类方法扩展MyFancyModule。导入到底是什么意思?您有任何示例代码吗?
包括MyFancyModule
作为示例方法
ExtendeMyFancyModule
用于类方法。请注意,不需要在上面使用
InstanceMethods
;我通常认为模式只是
模块条;包括def自身(klass);扩展(类方法);终止def normal_instance_方法;终止模块分类方法;def-class_方法;终止终止结束
@Phrogz:我知道。我只是觉得这样更合乎逻辑。在我的模块中,有两个子模块或没有子模块。请注意,不需要使用上面的
InstanceMethods
;我通常认为模式只是
模块条;包括def自身(klass);扩展(类方法);终止def normal_instance_方法;终止模块分类方法;def-class_方法;终止终止结束
@Phrogz:我知道。我只是觉得这样更合乎逻辑。在我的模块中,有两个子模块或没有子模块。