From: Josh Cheek Date: 2011-09-30T12:08:21+09:00 Subject: Re: Ruby mixin: extend or include? --0016364d1e65e33b2d04ae1fefe5 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Sep 29, 2011 at 9:48 PM, Jones Lee wrote: > I have following code: > > 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 > > In fact both 2 lines of test codes works. So is there any difference > between the way I use 'extend' and 'include' (inside a singleton class > of self)? > > -- > Posted via http://www.ruby-forum.com/. > > The only difference that I know of is how the hooks get called: module M def self.extended(obj) p "M is extended by #{obj.inspect} from line #{caller.first[/\d+/]}" end def self.included(klass) p "M is included in #{klass.inspect} from line #{caller.first[/\d+/]}" end end class C extend M class << self include M end end # >> "M is extended by C from line 12" # >> "M is included in # from line 14" As an aside, this is yet another argument against extending the object in the included hook. --0016364d1e65e33b2d04ae1fefe5--