Ruby mixin:扩展还是包含?

时间:2011-09-30 03:09:47

标签: ruby singleton metaclass mixins

我有以下代码:

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)?

1 个答案:

答案 0 :(得分:2)

不,这是一回事,但第一种方式更清洁。

相关问题