2011-09-30 47 views
2

我有以下代码:Ruby mixin:扩展还是包含?

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 

其实都2号线的测试代码工作。那么在我使用'extend'和'include'的方式(在单个类 的self内)之间是否有差异 ?

+2

查看有趣的讨论[在Ruby中包含和扩展有什么区别?](http://stackoverflow.com/questions/156362/what-is-the-difference-between-include-and-extend-在旁注) – mliebelt

回答

2

不,这是一样的事情,但第一种方法是更清洁。