【问题标题】:Ruby mixin: extend or include?Ruby mixin:扩展还是包含?
【发布时间】:2011-11-28 04:00:40
【问题描述】:

我有以下代码:

module CarHelper
  def call_helpline
    puts "Calling helpline..."
  end
end

class Car
  extend CarHelper
end

class Truck
  class << self
    include CarHelper
  end
end

# Test code
Car.call_helpline
Truck.call_helpline

事实上,两行测试代码都有效。那么有什么区别吗 在我使用“extend”和“include”的方式之间(在单例类中 self)?

【问题讨论】:

标签: ruby singleton metaclass mixins


【解决方案1】:

不,是一样的,但第一种方法更干净。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2016-02-24
    • 2018-09-30
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多