【问题标题】:self.included – including class methods from a module in Rubyself.included – 包含来自 Ruby 模块的类方法
【发布时间】:2017-12-19 21:31:19
【问题描述】:

我读过这篇文章:Ruby modules - included do end block - 但仍然不知道何时在模块中使用 self.included do ... end 块。

帖子说,当您包含模块时,块中的代码将运行,但如果包含模块的唯一目的是什么?无论如何都不需要运行该代码吗?为了运行该代码,不需要该块存在,对吗?

以下两者有什么区别:

module M
  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      scope :disabled, -> { where(disabled: true) }
    end
  end

  module ClassMethods
    ...
  end
end

对比

module M
  def self.some_class_method
     ...
  end

  scope :disabled, -> { where(disabled: true) }
end

【问题讨论】:

  • 听起来包含块将这些变量从另一个模块带到模块 M
  • 但是没有它就不行吗?
  • 只是尝试运行一个带有一些值的示例,看看它是否有效,请让我知道。 你改变了你的代码?!!!

标签: ruby-on-rails ruby


【解决方案1】:

这两个例子有什么区别?

第一个代码块将ClassMethods 中的类方法添加到包含类,并在其上调用scope 方法。第二个不做这些事情,将导致NoMethodError,因为模块没有scope 类方法。一旦包含模块,self.some_class_method 将在包含类中不可用。

有关模块包含如何在 Ruby 中工作的完整故事,请在此处阅读我的回答:
Inheriting class methods from modules / mixins in Ruby

如果要包含一个模块的唯一目的,self.included 的意义何在?

包含并不是模块的唯一目的。它们还用于其他事情,例如命名空间或简单地存储各种类方法,然后可在模块本身上调用。

为什么 Ruby 不自动包含类方法?

理论上 Ruby可以自动将模块中定义的所有类方法添加到包含类中,但实际上这不是一个好主意,因为您 将无法再选择 strong> 是否要包含类方法 — 每次都会包含所有类方法,无论它们是否打算包含在内。考虑这个例子:

module M
  def self.class_method
    "foo"
  end

  def self.configure_module
    # add configuration for this module
  end
end

class C
  include M
end

这里,configure_module 方法显然不应该被添加到C,因为它的目的是设置模块对象的配置。但是,如果我们对类方法进行自动包含,您将无法阻止它被包含。

但是所有的实例方法都已经包含了!那怎么办?

模块中的实例方法只有真正有用如果它们被包含在一个类中,因为模块不能有实例,只有类可以。因此,在一个模块中,每个实例方法都应该包含在某个工作的地方。

模块上的“类”方法不同,因为它可以在模块本身上调用,所以不管它是否也被添加到包含类中,它都可以正常使用。这就是为什么你在那里有一个选择会更好。

【讨论】:

  • 您能解释一下包含“仅使其实例方法可用”的原因吗?如果他们也提供类方法会不会更容易?这样做有什么问题?
  • 是不是因为include只在祖先查找中增加了实例方法,而类方法仍然属于那个模块?
  • 基本上是的,这就是我在“分类方法如何工作”部分中解释的内容。我现在正在写一个更详细的解释。
  • 在“为什么 Ruby 不自动执行此操作?”下添加了一些附加说明
  • 模块中的实例方法只有在包含在类中时才真正有用,因此我们希望将所有这些方法都包含在内。在第二个示例中(现在已删除 - 它比有用更令人困惑),我想您可以用 yes 回答所有这些问题,但是鉴于当前的类/模块,由此产生的行为将很难实现Ruby 的对象模型。
【解决方案2】:
module M
  # self.included is the hook which automatically runs when this module is included
  def self.included(base)
    puts 'this will be printed when this module will be included inside a class'
    base.extend ClassMethods
    base.class_eval do
      scope :disabled, -> { where(disabled: true) }
    end
  end

  def print_object_class
    self.class.name # here self will be the object of class which includes this module
  end

  module ClassMethods
    def print_class_name
      self.name # Here self would be the class which is including this module
    end
  end
end

我尝试修改上述module 以帮助您了解模块(关注点)如何有助于代码可重用性

self.included 是一个钩子,当模块包含在类中时会自动运行。

module ClassMethods 中声明的任何方法都将成为包括该模块在内的类的类方法

module ClassMethods 之外声明的任何方法都将成为包括该模块在内的类的实例方法

对于 Ex 假设有一个类 Product 并且你已经在其中包含了模块

class Product < ActiveRecord::Base
  include M
  puts 'after including M'
end

如果您在 Rails 控制台中尝试此示例,您会注意到,只要模块 M 包含在 class Product 中,就会包含模块运行的钩子和

this will be printed when this module will be included inside a class 打印在控制台上

之后after including M 将打印在控制台上。

你也可以试试下面的命令

Product.disabled # a scope with name 'disabled' is avaialble because of including the module M
Product.print_class_name # Outputs => 'Product' This method is available to class with the help of module M
Product.new.print_object_class #Outputs => 'Product'

它还提供可重用性,将这个模块 M 包含在任何类中,并且该类可以访问模块中描述的所有方法。

而您的第二个示例仅仅是基本模块的示例

module N
  def self.abc
    puts 'basic module'
  end
end

现在 abc 在模块中定义的方法可以仅使用此模块访问

N.abc # outputs 'basic module'

class Product < ActiveRecord::Base
  include  N
end

Product.abc #引发异常,在类 Product 上找不到方法 Product.new.abc #引发异常,在 Product 类的对象上找不到方法

我希望这可以帮助您更好地理解模块的概念。 如果您仍有任何疑问,请告诉我。

【讨论】:

  • 为什么 Product.abc #raise 一个异常,即使它被包含在内?是不是因为#abc还在模块N的范围内,所以Product不能调用它?
  • 所以类方法必须在 self.included 和 base.extend 块内,而实例方法可以正常定义?
  • 是的 abc 仍然只能通过模块访问。当我们包含一个模块时,该模块中的类方法不会包含在该类中。这些方法仅适用于模块。
  • @stackjlei - 你甚至可以在 self.included 范围内定义实例方法。正如我告诉你的那样,只要你在类中包含一个模块,就会调用 self.included 它可以用来为类定义scopes,或者当你包含一个模块时想要做某事。 module ClassMethods 也用于声明类方法 如果还有疑问请随时提出
  • @stackjlei 我刚刚添加了一个答案,详细解释了幕后发生的事情以及为什么我们首先拥有 self.included 块。
猜你喜欢
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 2016-02-24
相关资源
最近更新 更多