From: James Coglan Date: 2008-09-02T00:58:32+09:00 Subject: included() vs extended() ------=_Part_53001_23410792.1220285027753 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi list, Not sure if I should be asking this on the core list instead, but... I've a question about the included() and extended() hooks. Consider the following: # A little helper from _why class Object def metaclass class << self; self; end end end # A module with hooks module Hooks def self.included(base) puts "including M" end def self.extended(base) puts "extending using M" end def includes_hooks? true end end # A couple classes class Foo; end class Bar; end # Prints "extending using M" Foo.extend(Hooks) # Prints "including M" Bar.metaclass.send(:include, Hooks) # Both return true Foo.includes_hooks? Bar.includes_hooks? # Check inheritance tree Foo.metaclass.ancestors #=> [Hooks, Class, Module, Object, Kernel] Bar.metaclass.ancestors #=> [Hooks, Class, Module, Object, Kernel] So my question is: given that extend() is implemented (I think) using metclass.include(), and that the end result of both is identical, how come Hooks.included is not called when using extend()? It seems reasonable that both the included() and extended() hooks should be called in this situation, though I can just as well see the argument against doing this. Anyone care to enlighten/persuade me as to why this behaviour exists? -- James Coglan http://blog.jcoglan.com http://github.com/jcoglan ------=_Part_53001_23410792.1220285027753--