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)?
On Thu, Sep 29, 2011 at 9:48 PM, Jones L. [email protected] wrote:
end
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 #Class:C from line 14”
As an aside, this is yet another argument against extending the object
in
the included hook.