Ruby 中的 include 和 prepend

Ruby 中使用 mixin 优雅的解决了 multiple inheritance 问题。在 Java 世界中使用 interface 解决这个问题,在 Ruby 中使用 module,与 Java 中不同的是:Ruby 中的 module 不但可以定义接口,而且还能提供实现。

在 Ruby 提供 include 和 prepend 两种方式使用 module,而对 include 或者 prepend module 这种方式,Ruby 称为 mixin。本文将介绍这两种不同的 mixin 方式。

先认识一下 include 和 prepend

猜一下这段代码的输出:

class SuperSpeaker
  def speak
    puts 'Super class speaking...'
  end
end

module Chinese
  def speak
    puts '在说中文...'
    super
  end
end

module Thai
  def speak
    puts 'ฉันกำลังพูด...'
    super
  end
end

class Speaker < SuperSpeaker
  include Chinese
  prepend Thai

  def speak
    puts 'Subclass speaking...'
    super
  end
end

p Speaker.ancestors
speaker = Speaker.new
speaker.speak

这段代码将输出:

[Thai, Speaker, Chinese, SuperSpeaker, Object, Kernel, BasicObject]
ฉันกำลังพูด...
Speaker is speaking...
在说中文...
Super speaker is speaking...

通过输出我们可以看到:

  • include 会将 Chinese module 加入到 Speaker 的后面
  • prepend 会将 Thai module 加入到 Speaker 的前面

从 Ruby 的 Method lookup 机制理解 include 和 prepend

在理解 include 和 prepend 时,我们需要知道,当我在一个 instance 上调用方法时,都发生了什么?

  1. Ruby 中方法存在 Class 中,instance 只有变量没有方法。当在 instance 上调用方法时,会先从 instance 对应的 Class 中查找(先向右查找)
  2. 当无法在当前 Class 找到改方法时,Ruby 会按照继承链向上查找方法(后向上查找)

这个机制在 Ruby 中称为 Method lookup,一句话总结 Method lookup 机制:

『先向右,后向上』

在上段代码中,我们输出了 Speaker 的继承链:

p Speaker.ancestors
# => [Thai, Speaker, Chinese, SuperSpeaker, Object, Kernel, BasicObject]

Method lookup 过程是这样的:

method_lookup.png

从 Method lookup 角度来理解 include 和 prepend:

  • include 将 Chinese module 加入到 Speaker 继承链的上方
  • prepend 将 Thai module 加入到 Speaker 继承链的下方

这样也就解释了,为什么 Speaker#speak 会先输出泰文:『ฉันกำลังพูด...』。

使用 include 和 prepend 实现一个简单的 DSL

在 Rails controller 中,before_action 非常好用,这里我们使用 include 和 prepend 实现一个简单的 before_action:

module BeforeAction
  def self.included(klass)
    klass.extend(ClassMethods)
  end

  module ClassMethods
    def before_action(method_name, options)
      action_module = Module.new do
        send :define_method, options[:for] do |*args, &block|
          send method_name

          super(*args, &block)
        end
      end

      prepend action_module
    end
  end
end

class Speaker
  include BeforeAction

  before_action :chinese_self_intro, for: :speak

  def speak
    puts 'I am speaking...'
  end

  private

  def chinese_self_intro
    puts 'Hello, I come from china.'
  end
end

Speaker.new.speak

输出一下 Speaker 的继承链,便于我们理解 before_action 的原理:

p action_module.class_methods
#=> [:speak]
p Speaker.ancestors
#=> [#<Module:0x007fe1ca254be0>, Speaker, BeforeAction, Object, Kernel, BasicObject]
before_action.png
  1. 使用 include 将 BeforeAction mixin 到 Speaker 继承链上面,此时在定义 Speaker 时便可使用 before_action
  2. 在 before_action 中 prepend 将带有 speak 方法的 Anonymous module,将 Anonymous module 插入到继承链的底端。这样在调用 Speaker#speak 时,先调用 Anonymous module 中的 speak
  3. 在 Anonymous module 中的 speak 方法调用 for 参数指定的 chinese_self_intro
  4. 在 Anonymous module 中的 speak 方法的最后,使用 super(*arg, &block) 调用继承链上方的方法(即:Speaker#speak

总结

Ruby 中的 include 和 prepend 为 Ruby 提供了 mixin 机制,这个机制不但解决multiple inheritance 问题,而且为 Ruby 的 DSL 能力提供了强大的支持。

那么什么时候用 mixin,什么时候用 inheritance?

从一个 Java 程序员角度来讲,当你想定义 interface 时便可使用 mixin。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Ruby是一门单一继承的面向对象语言,那么在内部结构上,它是以object为根节点的树形结构的类图,那么我们在Ru...
    falm阅读 5,299评论 0 1
  • 最近复习了下 ruby 对象模型的知识,参照了 Ruby Metaprogramming,于是边看边做笔记,还是收...
    张羽辰阅读 3,508评论 0 4
  • 一、 让自己熟悉Ruby 1、理解 Ruby 中的 True 在 Ruby 中,除了 false 和 nil, 其...
    Sgemini阅读 3,892评论 0 1
  • 一、异同对比选择1、Python和ruby的相同点: * 都强调语法简单,都具有更一般的表达方式。python是缩...
    沃伦盖茨阅读 9,630评论 2 24
  • 本文有剧透,未观影者谨慎阅读。以下为正文。 导演让故事发生在民国末年。 号称是一所学校,教室由一座庙改建,一名校长...
    烧焦的小麦阅读 4,340评论 0 1

友情链接更多精彩内容